uint8_t TFlash_UnlockSequence(void) { uint32_t val, i; /* verify the result */ SWJ_ReadAP(MDM_IDR, &val); if ((val != KINETIS_KK_MCU_ID) && (val != KINETIS_KL_MCU_ID)) { printf("MCU_ID does not match!0x%X\r\n", val); return 1; } SWJ_ReadAP(MDM_STATUS, &val); /* flash in secured mode */ if (val & (1 << 2)) { printf("system is secured mode\r\n"); /* hold the device in reset */ SWJ_SetTargetState(RESET_HOLD); // SWJ_WriteAP(MDM_CTRL, 0x08); // DelayMs(20); // SWJ_SetTargetState(RESET_RUN); // SWJ_InitDebug(); // SWJ_SetTargetState(RESET_HOLD); /* write the mass-erase enable bit */ if(SWJ_WriteAP(MDM_CTRL, 1)) { printf("write mass erase enable failed\r\n"); return 1; } SWJ_ReadAP(MDM_CTRL, &val); printf("MDM_CTRL:0x%X\r\n", MDM_CTRL); while (1) { /* wait until mass erase is started */ if(SWJ_ReadAP(MDM_STATUS, &val)) { printf("read satatus failed\r\n"); return 2; } if (val & 1) break; } /* mass erase in progress */ while (1) { /* keep reading until procedure is complete */ if(SWJ_ReadAP(MDM_CTRL, &val)) return 3; if (val == 0) break; } } return 0; }
static int ShowID(void) { uint32_t id; SWJ_ReadDP(DP_IDCODE, &id); printf("DP-IDR:0x%X\r\n", id); SWJ_ReadAP(0x000000FC, &id); printf("AHB_AP_IDR:0x%X\r\n", id); printf("MEMID:0x%X\r\n", TFlashGetMemID()); printf("SDID:0x%X\r\n", TFlashGetSDID()); return 0; }
uint32_t TFlashGetMemID(void) { uint32_t id; SWJ_ReadAP(MDM_IDR, &id); return id; }
uint8_t TFlash_UnlockSequence(void) { uint32_t val; /* read the device ID */ if(SWJ_ReadAP(MDM_IDR, &val)) { printf("readID failed:0x%08X\r\n", val); return 1; } /* verify the result */ if ((val != KINETIS_KK_MCU_ID) && (val != KINETIS_KL_MCU_ID)) { printf("MCU_ID does not match!0x%X\r\n", val); return 1; } if(SWJ_ReadAP(MDM_STATUS, &val)) { printf("read mem_status error\r\n"); return 3; } printf("MEMSTATUS:0x%X\r\n", val); /* flash in secured mode */ // if (val & (1 << 2)) { printf("system is secured mode\r\n"); /* hold the device in reset */ //SWJ_SetTargetState(RESET_PROGRAM); SWJ_SetTargetState(RESET_HOLD); /* write the mass-erase enable bit */ if(SWJ_WriteAP(MDM_CTRL, 1)) { printf("write enable failed\r\n"); return 1; } while (1) { //SWJ_WriteAP(MDM_CTRL, 1); /* wait until mass erase is started */ if(SWJ_ReadAP(MDM_STATUS, &val)) { printf("read satatus failed\r\n"); return 2; } if (val & 1) break; } /* mass erase in progress */ while (1) { /* keep reading until procedure is complete */ if(SWJ_ReadAP(MDM_CTRL, &val)) return 3; if (val == 0) break; } } return 0; }