partial erase vs BLE stack

Hello,

I have been developed a log system in the internal flash. To do that I have been using the nrf storage library.

My first problem came up when I tried to use nrf_fstorage_erase for one single page; this operation is blocking the interrupts of some sensors, so I am losing some data. I calculated how much was the elapsed time during which I am blocked and I obtained around 150 ms (which is higher than the 85 ms that I found for a single page erase in the datasheet) which is strange to me.

As I cannot afford to lose the data during 150 ms, I have checked the partial erase feature. Following this idea, I developed the following function:

static void partial_erase(nrf_fstorage_t const* p_fstorage, uint32_t p_flash_start, uint32_t num_pages)
{
    ret_code_t err_code;
    
    /* Check that the address and size are aligned to the flash page size */
    if (p_flash_start % FLASH_PAGE_SIZE) {
        /* Handle unaligned address or size */
    }
  
    /* Enable the write access to the flash */
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
        
    /* Configure the partial erase duration */
    NRF_NVMC->ERASEPAGEPARTIALCFG = PARTIAL_ERASE_TIME_MS;

    /* Erase the specified number of pages */
    for (uint32_t i = 0; i < num_pages; i++) {
        /* Calculate the address of the page to erase */
        uint32_t page_address = p_flash_start + (i * FLASH_PAGE_SIZE);
    
        /* Specify the address of the page to partially erase */
        NRF_NVMC->ERASEPAGEPARTIAL = page_address;

        /* Wait for the erase operation to complete */
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
//        wait_for_flash_ready(&fstorage);
    }
    
    /* Disable the write access to the flash */
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
}

The problem here is that it looks incompatible with the parallel execution of BLE stack.

I got into the function "app_error_fault_handler" right after the execution of the line:

NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);

My understanding is that there is some conflict with the parallel BLE execution and partial erase, so I updated my function disabling/enabling temporarily the ble stack in this way:

static void pause_ble()
{
    nrf_sdh_disable_request();  
}

static void resume_ble()
{
    nrf_sdh_enable_request();

    // Wait for SoftDevice to be enabled
    while (!nrf_sdh_is_enabled()) {
         __WFE();
    }
    
    // Wait for HFCLK to be started
    while (nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY) != true) {}
}

static void partial_erase(nrf_fstorage_t const* p_fstorage, uint32_t p_flash_start, uint32_t num_pages)
{
    ret_code_t err_code;
    
    /* Check that the address and size are aligned to the flash page size */
    if (p_flash_start % FLASH_PAGE_SIZE) {
        /* Handle unaligned address or size */
    }

    pause_ble();
  
    /* Enable the write access to the flash */
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
        
    /* Configure the partial erase duration */
    NRF_NVMC->ERASEPAGEPARTIALCFG = PARTIAL_ERASE_TIME_MS;

    /* Erase the specified number of pages */
    for (uint32_t i = 0; i < num_pages; i++) {
        /* Calculate the address of the page to erase */
        uint32_t page_address = p_flash_start + (i * FLASH_PAGE_SIZE);
    
        /* Specify the address of the page to partially erase */
        NRF_NVMC->ERASEPAGEPARTIAL = page_address;

        /* Wait for the erase operation to complete */
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
//        wait_for_flash_ready(&fstorage);
    }
    
    /* Disable the write access to the flash */
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);      
    
    resume_ble();
}

After this modification, I got another issue: BLE_ERROR_NOT_ENABLED after the interrupt SWI2_EGU2_IRQn is triggered. It looks like there is some BLE operation before the BLE stack is enabled, but I don't know how to prevent the SWI2_EGU2_IRQn to raise in order to avoid the error.

Could you please provide me some suggestions or corrections of my code to make partial erase and BLE stack working?

Thanks!

Parents Reply Children
No Data
Related