<?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>fstorage event handler never fired for erase operation with 1 active BLE connection</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/70949/fstorage-event-handler-never-fired-for-erase-operation-with-1-active-ble-connection</link><description>Hello, I am trying to erase a page on the internal flash memory of the nRF52840. I am doing this as a BLE peripheral with 1 active connection. The problem I&amp;#39;m facing is that I am not getting any resulting feedback from fstorage after running (queuing</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 01 Feb 2021 09:03:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/70949/fstorage-event-handler-never-fired-for-erase-operation-with-1-active-ble-connection" /><item><title>RE: fstorage event handler never fired for erase operation with 1 active BLE connection</title><link>https://devzone.nordicsemi.com/thread/292146?ContentTypeID=1</link><pubDate>Mon, 01 Feb 2021 09:03:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8990aeac-c0a0-4b39-aee6-f962917eb893</guid><dc:creator>TSonono</dc:creator><description>&lt;p&gt;I was able to find the source of the issue. It was due to the fact that I had not linked the file&amp;nbsp;${SDK_ROOT}/components/softdevice/common/nrf_sdh_soc.c in to my project. Surprisingly, this does not result in a linking or compilie error... This made it quite hard to figure&amp;nbsp;out what was wrong.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Tofik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: fstorage event handler never fired for erase operation with 1 active BLE connection</title><link>https://devzone.nordicsemi.com/thread/291862?ContentTypeID=1</link><pubDate>Thu, 28 Jan 2021 18:16:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b352afe8-8699-4c6e-ab03-9d8721a1c2c8</guid><dc:creator>TSonono</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/dev_5F00_board.ld"&gt;devzone.nordicsemi.com/.../dev_5F00_board.ld&lt;/a&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/55144.sdk_5F00_config.h"&gt;devzone.nordicsemi.com/.../55144.sdk_5F00_config.h&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*======= Includes ==========================================================*/

#include &amp;quot;bsp_nrf_board.h&amp;quot;
#include &amp;quot;board.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;
#include &amp;quot;app_scheduler.h&amp;quot;
#include &amp;quot;mem_manager.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;

#include &amp;quot;key_tasks.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_fstorage.h&amp;quot;
#include &amp;quot;nrf_fstorage_sd.h&amp;quot;

#include &amp;quot;rtc.h&amp;quot;

#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;

/*======= Local Macro Definitions ===========================================*/

#define SCHEDULER_QUEUE_SIZE (20U)
#define MY_BLE_OBSERVER_PRIO 3
#define MY_BLE_CONN_CFG_TAG 1

#define FLASH_PAGE_SIZE     (0x00001000U)

#define STORAGE_NUM_PAGES   (6U)
#define STORAGE_END_ADDR    (0x000F2000U)
#define STORAGE_START_ADDR  (STORAGE_END_ADDR - ((STORAGE_NUM_PAGES)*(FLASH_PAGE_SIZE)))


#define STORAGE_STATE_IDLE      (0U)
#define STORAGE_STATE_WRITING   (1U)
#define STORAGE_STATE_ERROR     (2U)

/*======= Type Definitions ==================================================*/
/*======= Local function prototypes =========================================*/

static void bsp_isr_gpio_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
static void ble_stack_init(void);
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);
static void fs_evt_handler(nrf_fstorage_evt_t  *  evt);
static void hal_erase(uint32_t start_page, uint32_t num_pages);
static bool port_setup_storage(void);
static bool erase_something(void);

static void erase_something_task(void *event_data, uint16_t event_size);

/*======= Local variable declerations ========================================*/

NRF_FSTORAGE_DEF(nrf_fstorage_t m_fs_conf) =
{
    .start_addr = (const uint32_t )STORAGE_START_ADDR,
    .end_addr  = (const uint32_t )STORAGE_END_ADDR,
    .evt_handler   = fs_evt_handler,           // Function for event callbacks.
};

static volatile uint32_t m_storage_state;

/*======= Local function implementations =====================================*/

static bool erase_something(void)
{
    uint32_t erase_addr = STORAGE_START_ADDR;
    hal_erase(erase_addr, 1);

    return true;
}

int main(void)
{
    uint32_t ret;

    APP_SCHED_INIT(0, SCHEDULER_QUEUE_SIZE);

    APP_ERROR_CHECK(NRF_LOG_INIT(app_timer_cnt_get));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    bsp_init();
    nrf_mem_init();

    bsp_register_gpio_event_handler(bsp_isr_gpio_event_handler);

    rtc_init();

    ret = port_setup_storage();
    if (false == ret)
    {
        NRF_LOG_ERROR(&amp;quot;Storage initialization failed.\r\n&amp;quot;);
    }

    // erase_something();
    // NRF_LOG_INFO(&amp;quot;Added first user&amp;quot;);

#ifdef SOFTDEVICE_PRESENT
    ble_stack_init();
#endif


    while (1)
    {
        bsp_wdt_feed();
        app_sched_execute();
        bsp_sleep();
    }
}


static bool port_setup_storage(void)
{
#ifdef SOFTDEVICE_PRESENT
    ret_code_t err_code = nrf_fstorage_init(&amp;amp;m_fs_conf, &amp;amp;nrf_fstorage_sd, NULL);
#else
    ret_code_t err_code = nrf_fstorage_init(&amp;amp;m_fs_conf, &amp;amp;nrf_fstorage_nvmc, NULL);
#endif

    if (err_code != NRF_SUCCESS)
    {
        APP_ERROR_CHECK(err_code);
        NRF_LOG_ERROR(&amp;quot;nrf_fstorage_init() failed\r\n&amp;quot;);
    }

    m_storage_state = STORAGE_STATE_IDLE;

    return true;
}


static void hal_erase(uint32_t start_page, uint32_t num_pages)
{
    m_storage_state = STORAGE_STATE_WRITING;
    uint32_t m_fs_err_code = nrf_fstorage_erase(&amp;amp;m_fs_conf, start_page, num_pages, NULL);
    APP_ERROR_CHECK(m_fs_err_code);

    if (m_fs_err_code != NRF_SUCCESS)
    {
        m_storage_state = STORAGE_STATE_IDLE;
        NRF_LOG_ERROR(&amp;quot;Could not request erase flash page (0x%02x).\r\n&amp;quot;, m_fs_err_code);
    }

    while(nrf_fstorage_is_busy(&amp;amp;m_fs_conf))
    {
        bsp_wdt_feed();
    }

    while (m_storage_state == STORAGE_STATE_WRITING)
    {
        /* Feed watch dog */
        // TODO: We get stuck here when trying to claim over BLE (key)
        bsp_wdt_feed();
    }
    NRF_LOG_INFO(&amp;quot;ERASING COMPLETED!\r\n&amp;quot;);
}

static void erase_something_task(void *event_data, uint16_t event_size)
{
    erase_something();
    NRF_LOG_INFO(&amp;quot;Added first user&amp;quot;);
}

static void bsp_isr_gpio_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    if (action == NRF_GPIOTE_POLARITY_HITOLO)
    {
        switch (pin)
        {
            case 12:
                NRF_LOG_INFO(&amp;quot;Adding first user&amp;quot;);
                app_sched_event_put(NULL, 0, erase_something_task);
                break;
            default:
                APP_ERROR_CHECK_BOOL(0);
                break;
        }
    }
}

static void ble_stack_init(void)
{
    ret_code_t err_code;
    uint32_t ram_start = 0;

    err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code); /* TODO: Proper error handling */

    // Configure the BLE stack using the default settings.
    // Fetch the start address of the application RAM.
    err_code = nrf_sdh_ble_default_cfg_set(MY_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err_code); /* TODO: Proper error handling */

    // Enable BLE stack.
    err_code = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err_code); /* TODO: Proper error handling */

    // Register a handler for BLE events.
    NRF_SDH_BLE_OBSERVER(m_ble_observer, MY_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
}

static void fs_evt_handler(nrf_fstorage_evt_t * evt)
{
    if (evt-&amp;gt;result != NRF_SUCCESS)
    {
        APP_ERROR_CHECK(evt-&amp;gt;result);
        NRF_LOG_ERROR(&amp;quot;result: 0x%02x\r\n&amp;quot;, evt-&amp;gt;result);
        /* Write failed, go to error state. */
        m_storage_state = STORAGE_STATE_ERROR;
    }
    else
    {
        /* Write succeeded, go to idle state again. */
        m_storage_state = STORAGE_STATE_IDLE;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have basically put all relevant code in one line. It seems that a connection does even have to be established for this problem to occur, only the soft device being enabled makes the error occur as well. If we instead call erase_something in line 96 (commented out),&amp;nbsp; which is before the&amp;nbsp;BLE stack is enabled, it works fine.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have it configured so that pressing button 2 on the dev board puts a task on the scheduler queue which tries to erase a page.&lt;/p&gt;
&lt;p&gt;I have also attached the sdk_config and the linker file.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Tofik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: fstorage event handler never fired for erase operation with 1 active BLE connection</title><link>https://devzone.nordicsemi.com/thread/291679?ContentTypeID=1</link><pubDate>Thu, 28 Jan 2021 07:39:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:deda9643-6d46-483d-b5cf-88db2c3f0be5</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi Tofik,&lt;/p&gt;
[quote user="TSonono"]Could there be a problem with the SD observer for fstorage not being correctly created?[/quote]
&lt;p&gt;Yes, it could be. The nrf_fstorage_erase() function will return&amp;nbsp;NRF_SUCCESS as long as the erase operation is queued, so even if you get no error here that does not say much if there was a problem with the initialization. I would very much like to see all your code if possible? That would probably make things a lot clearer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: fstorage event handler never fired for erase operation with 1 active BLE connection</title><link>https://devzone.nordicsemi.com/thread/291598?ContentTypeID=1</link><pubDate>Wed, 27 Jan 2021 18:02:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:17ef36c8-c256-4876-a01e-7026c273273b</guid><dc:creator>TSonono</dc:creator><description>&lt;p&gt;Hi Einar,&lt;br /&gt;&lt;br /&gt;Thanks for your reply. The code&amp;nbsp;I have attached is being called from the main loop context on bare metal (I am using the app_scheduler in the nRF5 SDK), so I do not expect&amp;nbsp;this&amp;nbsp;to be a priority issue.&lt;/p&gt;
&lt;p&gt;I also checked if the data was in fact erased using the nrfjprog tool and with the memrd option. It seems like the page is in fact not erased.&lt;/p&gt;
&lt;p&gt;I tested putting a print at the&amp;nbsp;first line of&amp;nbsp;nrf_fstorage_sys_evt_handler() in nrf_fstorage_sd.c, and this print never occurs. Could there be a problem with the SD observer for fstorage not being correctly created?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Tofik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: fstorage event handler never fired for erase operation with 1 active BLE connection</title><link>https://devzone.nordicsemi.com/thread/291503?ContentTypeID=1</link><pubDate>Wed, 27 Jan 2021 13:22:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bcbf1454-e9e4-4ea2-80a6-a11232e4175f</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;There are two typical reasons for fstorage operations never completing or seemingly never completing. One is if the SoftDevice does not have time to schedule it in between BLE activity, but that should not be a problem here as you test with a long connection interval and a long supervision timeout. Another typical reason is if you wait for the operation to complete in a interrupt priority which is same or higher than that of the SoftDevice low priority. If that is the case, the event handler will never have time to run. I do not see enough of your code to see if that is the case. Can you clarify, or show more of your code so that I get the full picture?&lt;/p&gt;
&lt;p&gt;It would also be interesting if you could check with a debugger if the flash is actually erased or not, even though the event handler does not run. That would be tested by having some data in the specific flash page before, and then reading it out and verifying if it is all FF&amp;#39;s or not after the attempted erase operation.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>