Failed to allocate memory.

I am having nrf52833 module where I am attempting to run buttonless DFU for this I have made some changes in ble_app_uart_pca10100_s140.hex and have used 

  • SoftDevice as " s140_nrf52_7.0.1_softdevice.hex "
  • Bootloader as "secure_bootloader_ble_s140_pca10100.hex"
  • Have generated bootloader settings page by using command " 

    1. nrfutil settings generate --family NRF52 --application ble_app_uart_pca10100_s140.hex --application-version 3 --bootloader-version 2 --bl-settings-version 1 ble_settings.hex

    2. mergehex --merge ble_settings.hex secure_bootloader_ble_s140_pca10100.hex s140_nrf52_7.0.1_softdevice.hex ble_app_uart_pca10100_s140.hex --output ble_sd_dfu_settings_app.hex "

    After this I have flashed "ble_sd_dfu_settings_app.hex" But unable to see any BLE name flashing.Then I attempted to flash firstly "ble_settings.hex" , " s140_nrf52_7.0.1_softdevice.hex " , "secure_bootloader_ble_s140_pca10100.hex" and then debug the "ble_app_uart_pca10100_s140.hex" via SES. And I have noticed that it was showing "Failed to allocate memory when I send .zip via nrf connect"

Also after updating the .zip file once I am unable to send the .zip file again. Also it is showing  Secure DFU Service as "This service is empty"

 

I am unable to understand how to fix "Failed to allocate memory" error also what do DFU Service as "This service is empty" mean and how to resolve it?

Also when I flashed "ble_sd_dfu_settings_app.hex" why I was not able to see the BLE name? and when I flashed everything individually why does it show BLE name?

 

Parents
  • Hi,

    This should not be a problem if you use the sample out of the box, so I wonder which changes you have made? Can you share your code or a diff showing the changes?

    Also, which SDK version are you using? Based on the log and SoftDevice version I suspect SDK 16? If you are starting now, I would strongly suggest you use SDK 17.1.0 as that is compatible with the latest revision of our devices and also have many bug fixes etc. (Or even better, consider the nRF Connect SDK, ref nRF Connect SDK and nRF5 SDK statement).

  • As for the changes below are the changes that I have done in main.c -

    1. DFU-related #includes

    #include "nrf_power.h"
    #include "nrf_dfu_ble_svci_bond_sharing.h"
    #include "nrf_svci_async_function.h"
    #include "nrf_svci_async_handler.h"
    #include "ble_dfu.h"
    #include "nrf_bootloader_info.h"
    2. Added app_shutdown_handler
    /**@brief Handler for shutdown preparation.
     *
     * @details During shutdown procedures, this function will be called at a 1 second interval
     *          untill the function returns true. When the function returns true, it means that the
     *          app is ready to reset to DFU mode.
     *
     * @param[in]   event   Power manager event.
     *
     * @retval  True if shutdown is allowed by this power manager handler, otherwise false.
     */
    static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
    {
        switch (event)
        {
            case NRF_PWR_MGMT_EVT_PREPARE_DFU:
                NRF_LOG_INFO("Power management wants to reset to DFU mode.");
                // YOUR_JOB: Get ready to reset into DFU mode
                //
                // If you aren't finished with any ongoing tasks, return "false" to
                // signal to the system that reset is impossible at this stage.
                //
                // Here is an example using a variable to delay resetting the device.
                //
                // if (!m_ready_for_reset)
                // {
                //      return false;
                // }
                // else
                //{
                //
                //    // Device ready to enter
                //    uint32_t err_code;
                //    err_code = sd_softdevice_disable();
                //    APP_ERROR_CHECK(err_code);
                //    err_code = app_timer_stop_all();
                //    APP_ERROR_CHECK(err_code);
                //}
                break;
    
            default:
                // YOUR_JOB: Implement any of the other events available from the power management module:
                //      -NRF_PWR_MGMT_EVT_PREPARE_SYSOFF
                //      -NRF_PWR_MGMT_EVT_PREPARE_WAKEUP
                //      -NRF_PWR_MGMT_EVT_PREPARE_RESET
                return true;
        }
    
        NRF_LOG_INFO("Power management allowed to reset to DFU mode.");
        return true;
    }

    3. Added buttonless_dfu_sdh_state_observer()

    /**@brief Register application shutdown handler with priority 0.
     */
    NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0);
    
    static void buttonless_dfu_sdh_state_observer(nrf_sdh_state_evt_t state, void * p_context)
    {
        if (state == NRF_SDH_EVT_STATE_DISABLED)
        {
            // Softdevice was disabled before going into reset. Inform bootloader to skip CRC on next boot.
            nrf_power_gpregret2_set(BOOTLOADER_DFU_SKIP_CRC);
    
            //Go to system off.
            nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
        }
    }

    4. Added ble_dfu_buttonless_evt_handler()
    /* nrf_sdh state observer. */
    NRF_SDH_STATE_OBSERVER(m_buttonless_dfu_state_obs, 0) =
    {
        .handler = buttonless_dfu_sdh_state_observer,
    };
    
    static void ble_dfu_buttonless_evt_handler(ble_dfu_buttonless_evt_type_t event)
    {
        ret_code_t    err_code;
    
        switch (event)
        {
            case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE:
                NRF_LOG_INFO("Device is preparing to enter bootloader mode\r\n");
                break;
     
            case BLE_DFU_EVT_BOOTLOADER_ENTER:
                NRF_LOG_INFO("Device will enter bootloader mode\r\n");
                break;
     
            case BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED:
                NRF_LOG_ERROR("Device failed to enter bootloader mode\r\n");
                break;
            default:
                NRF_LOG_INFO("Unknown event from ble_dfu.\r\n");
                break;
        }
    }

    5. Added the following highlighted part in services_init()


    6. Added the following highlighted part in main()


    7. I have changed the following in sdk_config.h
    • #define BLE_DFU_ENABLED 1
    • #define NRF_PWR_MGMT_CONFIG_AUTO_SHUTDOWN_RETRY 1
    • #define NRF_SDH_BLE_SERVICE_CHANGED 1

    8. Added following in "Preprocessor Definitions"

    BL_SETTINGS_ACCESS_ONLY 

    NRF_DFU_TRANSPORT_BLE = 1

    9. Created a new folder within the project and added the following files-

    which SDK version are you using? I am using SDK16.

  • I have previously added the path which is like this -

    After building the app I do not get any warnings.

    but there are 2 notes -

  • I see. I don't understand this, then. Can you share the full project so that I can test on my end, along with instructions on how to test and reproduce the issue?

  •  I have attached the code kindly find that.

    To test you can directly flash "ble_sd_dfu_settings_app.hex" it is the merge hex of bootloader+softdevice+ble settings. Open nrf Connect app and connect to ble, you will see that BLE is connected but then you can see that "Secure DFU service" is empty and cannot dump the buttonless2.zip file via DFU

    Also the same can be checked via SES debug where you will see that while debugging "ble_app_uart" it says "Failed to allocate memory"

    /cfs-file/__key/communityserver-discussions-components-files/4/5873.ble_5F00_app_5F00_uart.zip

    /cfs-file/__key/communityserver-discussions-components-files/4/Bootloader_5F00_Softdevice.zip

    /cfs-file/__key/communityserver-discussions-components-files/4/buttonless2.zip.....

  • Hi,

    The code that prints "Failed to allocate memory" is written by you and not found in any SDK code. It comes because you are using malloc, but have not added any support for dynamic memory. I do not understand why you do this though, as there does not seem to be any reason for using dynamic memory here? (Generally it is best avoided, and particularily if there is no good reason for using it, as seems the case here).

    Regarding the service not working the problem is easy to spot when you check your errror codes. I see you commended out the APP_ERROR_CHECK after the call to ble_dfu_buttonless_init(), and this does not fix the issue - only masks it. Include the call, and you should see something like this:

    <error> app: ERROR 4 [NRF_ERROR_NO_MEM] at /home/eith/src/nrf5sdk/nRF5_SDK_16.0.0_98a08e2/examples/other_projects/ble_app_uartTest_327723/main.c:497
    PC at: 0x00031451

    Looking at the code this tells us that you got NRF_ERROR_NO_MEM returned from ble_dfu_buttonless_init(). So you know you need to debug there. And if you debug there you will see that the call to sd_ble_uuid_vs_add() fails. This is because NUS was added earlier, but in your sdk_config.h NRF_SDH_BLE_VS_UUID_COUNT is set to 1. This needs to be set to 2. Then you should run again, and you will get this error, as the SoftDevice now needs more RAM:

    <warning> nrf_sdh_ble: Insufficient RAM allocated for the SoftDevice.
    <warning> nrf_sdh_ble: Change the RAM start location from 0x20002AE8 to 0x20002AF8.
    <warning> nrf_sdh_ble: Maximum RAM size for application is 0x1D508.
    <error> nrf_sdh_ble: sd_ble_enable() returned NRF_ERROR_NO_MEM.
    <error> app: ERROR 4 [NRF_ERROR_NO_MEM] at /home/eith/src/nrf5sdk/nRF5_SDK_16.0.0_98a08e2/examples/other_projects/ble_app_uartTest_327723/main.c:695
    PC at: 0x0003189F

    Then you adjust the linker configuration as stated in the error message. And with that, it should work. A very important takeaway here is to not ignore errors.

  • Thank you so much for your effort I will follow what you have told me.  

Reply Children
No Data
Related