/********************************************************** * This function will verify that the target is unlocked * by trying to access the AHB-AP. It will also check that * the flash is erased because if we tried to unlock a * already unlocked chip (e.g we lost debug access because * the debug pins are disabled or we enter EM4 too fast) * the AHB-AP would already be available. **********************************************************/ void verifyUnlockedStatus(void) { int retry = CONNECT_RETRY_COUNT; bool success = false; uint32_t apId; while ( retry > 0 ) { TRY delayUs(200); initDp(); apId = readApId(); /* Verify that target is unlocked by reading the AHB-AP ID */ if ( verifyAhbApId(apId) ) { initAhbAp(); /* Verify that flash is erased */ verifyFlashIsErased(); /* If we get here, the target is unlocked */ success = true; } CATCH /* Do nothing on error. We retry */ ENDTRY /* If unlocked return from here since we cannot * return from within a TRY block */ if ( success ) { return; } retry--; } /* Raise an exception if device is NOT unlocked */ RAISE(SWD_ERROR_UNLOCK_FAILED); }
/********************************************************** * Performs the initialization sequence on the SW-DP. * After this completes the debug interface can be used. * Raises an exception on any error during connection. **********************************************************/ void connectToTarget(void) { uint32_t dpId,apId; delayUs(500); printf("Connecting to target...\n"); dpId = initDp(); /* Verify that the DP returns the correct ID */ if ( !verifyDpId(dpId) ) { printf("Read IDCODE = 0x%.8x\n", dpId); RAISE(SWD_ERROR_INVALID_IDCODE); } /* Verify that the AP returns the correct ID */ int retry = AHB_IDR_RETRY_COUNT; while ( retry > 0 ) { apId = readApId(); if ( verifyAhbApId(apId) ) { /* Success. AHB-AP registers found */ break; } retry--; } /* In case we did NOT find the AHB-AP registers check * if the chip is locked or if some other error ocurred. */ if ( !verifyAhbApId(apId) ) { /* If the ID is from AAP, the MCU is locked. * Debug access is not available. */ if ( apId == EFM32_AAP_ID ) { RAISE(SWD_ERROR_MCU_LOCKED); } else { /* The AP ID was not from AAP nor AHB-AP. This is an error. */ printf("Read IDR = 0x%.8x\n", apId); RAISE(SWD_ERROR_INVALID_IDR); } } /* Set up parameters for AHB-AP. This must be done before accessing * internal memory. */ initAhbAp(); /* * On Zero Gecko it is possible that the device is locked * even though we reach this point. We have to see if * we can access the AAP registers which are memory mapped * on these devices. The function will raise an exception * if the device is a Zero Gecko and is currently locked. */ if ( apId == EFM32_AHBAP_ID_2 ) { checkIfZeroGeckoIsLocked(); } haltTarget(); /* Get device name */ char deviceName[30]; getDeviceName(deviceName); printf("Target is %s\n", deviceName); /* Read the unique ID of the MCU */ uint64_t uniqueId = readUniqueId(); printf("Unique ID = 0x%.16llx\n", uniqueId); }