FLASH write process stuck when softdevice present

I'm developing a BLE application on nRF52840, where several characteristics correspond to several parameters. I need to read and validate parameters stored in flash when the power is turned on, and then save the runtime parameters to FLASH when the BLE connection is disconnected. The first step - reading and validating parameters from flash at power-on - works normally, but when it comes to saving the runtime parameters to FLASH upon BLE disconnection, the program freezes. Can anyone tell me why this is happening?

Since reading and validating parameters stored in flash is performed before advertising begins at power-on, I think this issue might be related to the SoftDevice.

Here is the snippet of my code.

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t err_code = NRF_SUCCESS;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected. Check updated parameter.");
            flash_params_t current_params = parameters_to_flash_param(m_operation_mode, m_sensing_period, m_reset_flag, m_axis_sel, &m_error_detect_params);
            flash_mgr_update_param(&current_params);
            NRF_LOG_INFO("Update parameter done.");
            // LED indication will be changed when advertising starts.
            break;

ret_code_t flash_mgr_update_param(flash_params_t* current_params)
{
    ret_code_t err_code;
    
    // Parameter validation
    if (current_params == NULL) {
        return NRF_ERROR_INVALID_PARAM;
    }
    
    if (!is_params_valid(current_params)) {
        return NRF_ERROR_INVALID_PARAM;
    }

    // Read existing parameters from flash
    err_code = read_all_params(&read_params);
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    // Compare current parameters with flash parameters
    if (memcmp(current_params, &read_params, sizeof(flash_params_t)) == 0) {
        // Parameters are identical, no need to update
        NRF_LOG_INFO("Parameters unchanged, skipping update");
        return NRF_SUCCESS;
    }

    // Parameters are different, update flash
    NRF_LOG_INFO("Updating parameters in flash");
    
    // Copy current parameters to write buffer
    memcpy(&write_params, current_params, sizeof(flash_params_t));
    
    // Write parameters to flash
    err_code = write_all_params(&write_params);
    APP_ERROR_CHECK(err_code);

    return err_code;
}

static ret_code_t read_all_params(flash_params_t *params_data)
{   
    return nrf_fstorage_read(&fstorage, FLASH_PAGE_ADDR, params_data, sizeof(flash_params_t));
}

static ret_code_t write_all_params(const flash_params_t *params)
{    
    ret_code_t err_code;
    
    // erase the page
    err_code = nrf_fstorage_erase(&fstorage, FLASH_PAGE_ADDR, 1, NULL);
    APP_ERROR_CHECK(err_code);
    
    // wait for erasing to complete
    while (nrf_fstorage_is_busy(&fstorage))
    {
        nrf_delay_ms(1);
    }
    
    // write params to flash
    err_code = nrf_fstorage_write(&fstorage, FLASH_PAGE_ADDR, params, sizeof(flash_params_t), NULL);
    APP_ERROR_CHECK(err_code);
    
    return err_code;
}

Parents Reply Children
No Data
Related