The five second delay is normal, and annoying. After bootloading I always disconnect the cable (which in our hardware is connected to the boot pin) then cycle power.
It is imperative that NMI be disabled for any of this to work correctly.
This is my flash config area and the function below, is the key to making it work correctly:
/* Flash configuration field: */
const uint8_t _kfcf[] __attribute__( ( section( ".kinetis_flash_config_field" ) ) ) =
{ [Not my real key]
0x33U, /**< Backdoor Comparison Key 3., offset: 0x0 NV_BACKKEY3 */
0x22U, /**< Backdoor Comparison Key 2., offset: 0x1 NV_BACKKEY2 */
0x41U, /**< Backdoor Comparison Key 1., offset: 0x2 NV_BACKKEY1 */
0x40U, /**< Backdoor Comparison Key 0., offset: 0x3 NV_BACKKEY0 */
0x27U, /**< Backdoor Comparison Key 7., offset: 0x4 NV_BACKKEY7 */
0x66U, /**< Backdoor Comparison Key 6., offset: 0x5 NV_BACKKEY6 */
0x95U, /**< Backdoor Comparison Key 5., offset: 0x6 NV_BACKKEY5 */
0x84U, /**< Backdoor Comparison Key 4., offset: 0x7 NV_BACKKEY4 */
0xFFU, /**< Non-volatile P-Flash Protection 1 - Low Register, offset: 0x8 NV_FPROT3 */
0xFFU, /**< Non-volatile P-Flash Protection 1 - High Register, offset: 0x9 NV_FPROT2 */
0xFFU, /**< Non-volatile P-Flash Protection 0 - Low Register, offset: 0xA NV_FPROT1 */
0xFFU, /**< Non-volatile P-Flash Protection 0 - High Register, offset: 0xB NV_FPROT0 */
/*
* FTFA_FSEC 0x040C:
*
* Note: The security features apply only to external
* accesses: debug. CPU accesses to the flash
* are not affected by the status of FTFA_FSEC.
*
* In the unsecured state, all flash commands are available on the
* programming interfaces either from the debug port (SWD) or user
* code execution. When the flash is secured (FTFA_FSEC[SEC] = 00,
* 01, or 11), the programmer interfaces are only allowed to
* launch mass erase operations. Additionally, in this mode, the
* debug port has no access to memory locations.
*
* KEYEN MEEN FSLACC SEC
* 7 6 5 4 3 2 1 0
*
* KEYEN = 0x01 Backdoor Disabled.
* KEYEN = 0x10 Backdoor Enabled.
*
* MEEN = 0x10 Mass Erase Disabled. All others Mass Erase Enabled.
*
* FSLACC Factory Security Level Access Code
*
* FSLACC = 0x01 or 0x11 Factory access granted. Others denied.
*
* SEC Flash Secutiry
*
* SEC = 0x10 Unsecure (0x7EU) which is the factory shipping state. 0xFEU is also unsecured which is what flash-erase-all-unsecure does.
*/
//0x7EU, /**< Non-volatile Flash Security Register, offset: 0xC NV_FSEC. Backdoor Disabled, Unsecured */
//0xBEU, /**< Non-volatile Flash Security Register, offset: 0xC NV_FSEC. Backdoor enabled, Unsecured */
0xBFU, /**< Non-volatile Flash Security Register, offset: 0xC NV_FSEC. Backdoor enabled, Secured */
/*
* FTFA_FOPT 0x04D:
* 7:BOOTSRC_SEL1 0x80U
* 6:BOOTSRC_SEL0 0x40U
* 00 = Flash, 01 = Reserved, 10/11 = ROM
*
* 5:NV_FOPT_FAST_INIT_MASK 0x20U
* 1 = Fast
*
* 4:NV_FOPT_LPBOOT1_MASK 0x10U
* See bit zero
*
* 3:NV_FOPT_RESET_PIN_CFG_MASK 0x08U
* 0 = Disabled Reset.
*
* 2:V_FOPT_NMI_DIS_MASK 0x04U
* 0 = Disable NMI
*
* 1:BOOTPIN_OPT 0x02U
* 0 = Force boot from ROM if BOOTCFG0 asserted.
*
* 0:NV_FOPT_LPBOOT0_MASK 0x01u
* 00 /8 VLPR on exit reset
* 01 /4 VLPR on exit reset
* 10 /2 RUN on exit
* 11 /1 RUN on exit
*
*/
0x38U, /**< Non-volatile Flash Option Register, offset: 0xD NV_FOPT Disable NMI pin. Boot from Flash not ROM */
0xFFU, /* Not used. Required to hold position to align vector table correctly */
0xFFU
};
You quested errata:
static void vectors_bootloader_mr_setup( void );
static void vectors_bootloader_mr_setup( void )
{
/*
* RCM_MR Indicates the boot source, the boot source remains set
* until the next System Reset or software can write logic one to
* clear the corresponding mode bit. While either bit is set the
* NMI input is disabled and the vector table is relocated to the
* ROM base address at 0x1C00_0000. These bits should be cleared by
* writing logic one before executing any code from either Flash or
* SRAM. [Doesn't actually make sense, is what the data sheet says.]
*
* A reset is forced to clear out anything that the ROM
* bootloader did, so we are sure we have the data sheet reset
* values. [Which are Vcc Rise not all reset sources.]
*
* This method works around the buggy KL43/27/17 bootloaders as
* described in: "Problem Analysis and solutions for booting from
* ROM BOOTLOADER in KL series".
*/
if( 0U != (RCM_MR & RCM_MR_BOOTROM_MASK) )
{
RCM_MR = RCM_MR_BOOTROM_MASK; /* Clear the bits that indicated a bootlaoder boot via ROM */
RCM_FM = 0U; /* Boot from Flash not ROM on next reset */
SCB_AIRCR = (SCB_AIRCR_VECTKEY(0x05FAU) | SCB_AIRCR_SYSRESETREQ_MASK); /* Force a Software Reset */
}
}
void _reset_init(void)
{
irq_disable(); /* Did bootloader leave IRQs on perhaps? */
bootloader_mr_setup();
SCB_VTOR = (uint32_t) interrupt_vector_table;
sync_barrier_data();
...