/**************************************************************************** **************************************************************************** Erase/Program Kinetis K40 flash : INPUT : OUTPUT : RETURN error code ****************************************************************************/ static BDI_InitTypeT initListK40[] = { {BDI_INIT_ARM11_WGPR, 8006, 100 }, // Wakeup time after reset released {BDI_INIT_ARM11_WGPR, 8012, 1 }, // memory access via AHB {BDI_INIT_ARM11_WGPR, 8013, 1 }, // 8 TCK's access delay {BDI_INIT_ARM11_WGPR, 13, 0x2000fffc}, // set SP to top of internal SRAM {BDI_INIT_ARM11_WM32, 0x4001F004, 0x00080000}, // FMC_PFB0CR: invaliate and disable cache // Disable Watchdog via code execution {BDI_INIT_ARM11_WGPR, 0, 0x40052000}, // WDOG register base address {BDI_INIT_ARM11_WGPR, 1, 0xC520 }, // unlock code A {BDI_INIT_ARM11_WGPR, 2, 0xD928 }, // unlock code B {BDI_INIT_ARM11_WGPR, 3, 0x01D2 }, // New value for WDOG_STCTRLH {BDI_INIT_ARM11_WM16, 0x20000000, 0x81c1 }, // strh r1, [r0, #14] {BDI_INIT_ARM11_WM16, 0x20000002, 0x81c2 }, // strh r2, [r0, #14] {BDI_INIT_ARM11_WM16, 0x20000004, 0x8003 }, // strh r3, [r0, #0] {BDI_INIT_ARM11_WM16, 0x20000006, 0xbe00 }, // bkpt 0x0000 {BDI_INIT_ARM11_EXEC, 0x20000000 }, // execute disable code }; static int StartupK40(void) { int result; /* reset and init target */ result = BDI_TargetStartup(100, // 100ms reset time // (0L << 16) | (11L << 8) | 2, // BDI2000: LE, Cortex-M4, 8 MHz (0L << 16) | (11L << 8) | 4, // BDI3000: LE, Cortex-M4, 8MHz sizeof initListK40 / sizeof initListK40[0], initListK40); return result; } /* StartupK40 */ static int ProgramK40(void) { int result; DWORD addr; DWORD errorAddr; /* reset and init target */ result = BDI_TargetStartup(100, // 100ms reset time // (0L << 16) | (11L << 8) | 2, // BDI2000: LE, Cortex-M4, 8 MHz (0L << 16) | (11L << 8) | 4, // BDI3000: LE, Cortex-M4, 8MHz sizeof initListK40 / sizeof initListK40[0], initListK40); if (result != BDI_OKAY) return result; /* setup flash type */ result = BDI_FlashSetType(BDI_FLASH_FTFL, 0x40000, // 256K 0, 32, 0x20000000); // workspace internal SRAM if (result != BDI_OKAY) return result; /* erase a 16K sector */ addr = 0x00000000; printf("erasing block at 0x%08lx ... ", addr); result = BDI_FlashErase(BDI_ERASE_BLOCK, addr); if (result != BDI_OKAY) return result; printf("passed\n"); /* program/verify from a file */ printf("programming..."); result = BDI_FlashWriteBinary("E:/Cygwin/home/bdidemo/arm11/k40x256.bin", 0x00000000, &errorAddr); if (result != BDI_OKAY) return result; printf("passed\n"); printf("verifing... "); result = BDI_VerifyBinary("E:/Cygwin/home/bdidemo/arm11/k40x256.bin", 0x00000000, &errorAddr); printf("passed\n"); return result; } /* ProgramK40 */