This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF51822 Secure Bootloader with app_timer

Hello everybody,

i want to add a timer to my secure bootloader example, so it waits some time and then, if no DFU is in progress, starts the beacon apllication automatic if there is one available.

I tried this by using the app_timer module, but it seems to crash the bootloader and i cant connect to it anymore.

These are some code snippets:

nrf_dfu.c

APP_TIMER_DEF(m_start_timer_id);

nrf_dfu.c in nrf_dfu_init()

    app_timer_create(&m_start_timer_id,
                                        APP_TIMER_MODE_SINGLE_SHOT,
                                        start_timer_handler);

app_timer_start(m_start_timer_id, 0x000FFF, start_timer_handler)

dfu_req_handling.c in nrf_dfu_req_handler_on_req()

app_timer_stop(m_start_timer_id);

I was reading the thread about using a Watchdog timer, but i think i cant use that, because it resets the device and starts the booloader again and again because i modified it to be buttonless.

I am using SDK12.3 and an NRF51822 XXAC chip

Thanks for your help!

  • What do you mean by crash? Is it advertising, but you can't connect? Or nothing? Have you tried to debug? There is a debug project in the SDK. Is APP_ERROR_CHECK() called with an error somewhere? See this. Also please consider uploading your complete project so I can test it here.

  • I haven't tested this, but something like this may work.

    Don't do nrf_dfu_enter_check(), but set enter_bootloader_mode = 1;

    Then you change BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED to whatever advertising timeout you want (max 180 s).

    When you get the BLE_GAP_EVT_TIMEOUT event with src == BLE_GAP_TIMEOUT_SRC_ADVERTISING you call ble_dfu_transport_close()

  • Thank you! That works so far, the only struggle i got now is to start the application after the bootloader times out, or restart the bootloader if there is no application. I tried this by calling nrf_dfu_init() right after ble_dfu_transport_close() but it just stops working. I will try to get the debugging to work, thanks for that link!

  • I did not get far with the debugger yet, my app_error files are different from those in the tutorial.

    This is my timeout event handler now:

    switch (p_ble_evt->header.evt_id)
        {
    			case BLE_GAP_EVT_TIMEOUT:
    				 if (p_ble_evt->evt.gatts_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING )
                {
    			nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
    			nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
    			nrf_delay_ms(500);
                  //  ble_dfu_transport_close();
    							
    				if (!nrf_dfu_app_is_valid())
                                    {
    					 sd_nvic_SystemReset();   
    	                        	}
    		
    		nrf_bootloader_app_start(MAIN_APPLICATION_START_ADDR);
    		
    							nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
    							nrf_gpio_pin_set(ADVERTISING_LED_PIN_NO);
    							
    						
                }
    			break;
    

    If there is no application it keeps resetting after timeout. If there is an application both LEDs remain bright but no advertising anymore, either BL nor Beacon app. Have a nice weekend!

  • Hi Sam,

    I suggest not to call nrf_bootloader_app_start() inside an event handler. You can try to set a flag in the event and then in the main loop wait_for_event() you execute nrf_bootloader_app_start().

Related