Skip to content

Commit dad8e10

Browse files
committed
bootloader_app_start() correct set
- use sd vector table set if existed - otherwise use mbr to set to application (0x1000)
1 parent f12c837 commit dad8e10

File tree

3 files changed

+43
-46
lines changed

3 files changed

+43
-46
lines changed

lib/sdk11/components/libraries/bootloader_dfu/bootloader.c

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@
3636
#include "tusb.h"
3737
#endif
3838

39-
/**< Maximum number of interrupts available. (from IRQn_Type) */
40-
#if defined(NRF52832_XXAA)
41-
#define MAX_NUMBER_INTERRUPTS 39
42-
#elif defined(NRF52840_XXAA) || defined(NRF52833_XXAA)
43-
#define MAX_NUMBER_INTERRUPTS 48
44-
#endif
45-
4639
/**@brief Enumeration for specifying current bootloader status.
4740
*/
4841
typedef enum
@@ -342,45 +335,42 @@ uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms)
342335
return err_code;
343336
}
344337

345-
346-
/**@brief Function for disabling all interrupts before jumping from bootloader to application.
347-
*/
348-
static void interrupts_disable(void)
338+
void bootloader_app_start(void)
349339
{
350-
for (uint32_t irq = 0; irq < MAX_NUMBER_INTERRUPTS; irq++)
351-
{
352-
NVIC_DisableIRQ((IRQn_Type)irq);
353-
}
354-
}
355-
356-
357-
void bootloader_app_start(uint32_t app_addr)
358-
{
359-
#ifdef SOFTDEVICE_PRESENT
360-
// If the applications CRC has been checked and passed, the magic number will be written and we
361-
// can start the application safely.
362-
APP_ERROR_CHECK ( sd_softdevice_disable() );
340+
// If the applications CRC has been checked and passed, the magic number will be written and we
341+
// can start the application safely.
342+
if ( is_sd_existed() ) sd_softdevice_disable();
343+
344+
// Disable all interrupts
345+
NVIC->ICER[0]=0xFFFFFFFF;
346+
NVIC->ICPR[0]=0xFFFFFFFF;
347+
#if defined(__NRF_NVIC_ISER_COUNT) && __NRF_NVIC_ISER_COUNT == 2
348+
NVIC->ICER[1]=0xFFFFFFFF;
349+
NVIC->ICPR[1]=0xFFFFFFFF;
363350
#endif
364351

365-
interrupts_disable();
352+
// default to boot right after MBR
353+
uint32_t app_addr = MBR_SIZE;
366354

367-
#if 0 // may need set forward irq
355+
if ( is_sd_existed() )
356+
{
357+
app_addr = SD_SIZE_GET(MBR_SIZE);
358+
sd_softdevice_vector_table_base_set(app_addr);
359+
}else
360+
{
368361
sd_mbr_command_t command =
369362
{
370-
.command = SD_MBR_COMMAND_IRQ_FORWARD_ADDRESS_SET,
371-
.params.irq_forward_address_set.address = MBR_SIZE,
363+
.command = SD_MBR_COMMAND_IRQ_FORWARD_ADDRESS_SET,
364+
.params.irq_forward_address_set.address = app_addr,
372365
};
373366

374367
sd_mbr_command(&command);
375-
#endif
376368

377-
#ifdef SOFTDEVICE_PRESENT
378-
APP_ERROR_CHECK( sd_softdevice_vector_table_base_set(app_addr) );
379-
#else
380-
SCB->VTOR = app_addr;
381-
#endif
369+
// VTOR as last resource ?
370+
// SCB->VTOR = app_addr;
371+
}
382372

383-
bootloader_util_app_start(app_addr);
373+
bootloader_util_app_start(app_addr);
384374
}
385375

386376

lib/sdk11/components/libraries/bootloader_dfu/bootloader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms);
5252
*
5353
* @param[in] app_addr Address to the region where the application is stored.
5454
*/
55-
void bootloader_app_start(uint32_t app_addr);
55+
void bootloader_app_start(void);
5656

5757
/**@brief Function for retrieving the bootloader settings.
5858
*

src/main.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ int main(void)
189189
if (dfu_start) NRF_POWER->GPREGRET = 0;
190190

191191
// Save bootloader version to pre-defined register, retrieved by application
192+
// TODO move to CF2
192193
BOOTLOADER_VERSION_REGISTER = (MK_BOOTLOADER_VERSION);
193194

194195
board_init();
@@ -214,7 +215,7 @@ int main(void)
214215
// DFU + FRESET are pressed --> OTA
215216
_ota_dfu = _ota_dfu || ( button_pressed(BUTTON_DFU) && button_pressed(BUTTON_FRESET) ) ;
216217

217-
bool const valid_app = bootloader_app_is_valid(DFU_BANK_0_REGION_START);
218+
bool const valid_app = bootloader_app_is_valid();
218219
bool const just_start_app = valid_app && !dfu_start && (*dbl_reset_mem) == DFU_DBL_RESET_APP;
219220

220221
if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET())
@@ -282,20 +283,24 @@ int main(void)
282283
// Reset Board
283284
board_teardown();
284285

285-
// Jump to application if valid
286-
if (bootloader_app_is_valid(DFU_BANK_0_REGION_START) && !bootloader_dfu_sd_in_progress())
286+
/* Jump to application if valid
287+
* "Master Boot Record and SoftDevice initializaton procedure"
288+
* - SD_MBR_COMMAND_INIT_SD (if not already)
289+
* - sd_softdevice_disable()
290+
* - sd_softdevice_vector_table_base_set(APP_ADDR)
291+
* - jump to App reset
292+
*/
293+
294+
if (bootloader_app_is_valid() && !bootloader_dfu_sd_in_progress())
287295
{
288-
#ifdef SOFTDEVICE_PRESENT
289296
// MBR must be init before start application
290-
if ( !sd_inited ) softdev_mbr_init();
291-
#endif
297+
if ( is_sd_existed() && !sd_inited ) softdev_mbr_init();
292298

293299
// clear in case we kept DFU_DBL_RESET_APP there
294300
(*dbl_reset_mem) = 0;
295301

296-
// Select a bank region to use as application region.
297-
// @note: Only applications running from DFU_BANK_0_REGION_START is supported.
298-
bootloader_app_start(DFU_BANK_0_REGION_START);
302+
// start application
303+
bootloader_app_start();
299304
}
300305

301306
NVIC_SystemReset();
@@ -323,7 +328,9 @@ void adafruit_factory_reset(void)
323328
#endif
324329

325330
/**
326-
* Initializes the SoftDevice and the BLE event interrupt.
331+
* Initializes the SotdDevice by following SD specs section
332+
* "Master Boot Record and SoftDevice initializaton procedure"
333+
*
327334
* @param[in] init_softdevice true if SoftDevice should be initialized. The SoftDevice must only
328335
* be initialized if a chip reset has occured. Soft reset (jump ) from
329336
* application must not reinitialize the SoftDevice.

0 commit comments

Comments
 (0)