/********************************************************** * Performs AAP Window Expansion. This function will reset * target, send the AAP expansion sequence, initialize the * debug interface and verify that AAP can be accessed. * It will try several delays between reset is released * and reading the AAP in case the reset line has a slow * ramp-up. * * After this function completes the AAP registers are * available. If it fails to access the AAP, it will * throw an exception. * **********************************************************/ void performAapExpansion(void) { uint32_t dpId, apId; int i,j; bool success = false; for ( i=0; i<AAP_EXPANSION_RETRY_COUNT; i++ ) { /* Pull reset pin low */ GPIO_PinOutClear((GPIO_Port_TypeDef)RESET_PORT, RESET_PIN); SWCLK_CLR(); delayMs(50); /* Send the AAP Window expansion sequence */ aapExtensionSequence(); /* Release reset */ delayUs(10); GPIO_PinOutSet((GPIO_Port_TypeDef)RESET_PORT, RESET_PIN); /* Try different delays in case of slow reset ramp */ for ( j=0; j<i; j++ ) { delayUs(10); } /* Connect to SW-DP */ TRY dpId = initDp(); apId = readApId(); if ( verifyDpId(dpId) && apId == EFM32_AAP_ID ) { /* Success! AAP registers can now be accessed. * We cannot return in the middle of a TRY block. * Set flag here and return after ENDTRY */ success = true; } CATCH /* Do nothing in case of error. We retry. */ ENDTRY /* Return if we found the AAP registers*/ if ( success ) { printf("AAP registers found\n"); return; } } /* Failed to get access to AAP registers. Raise an error. */ RAISE(SWD_ERROR_AAP_EXTENSION_FAILED); }
/********************************************************** * 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); }
int main() { while (scanf("%d", &n) != EOF) { for (i = 1; i < n + 1; i++) { scanf("%d", &valueArr[i]); } initDp(); int t; scanf("%d", &t); while (t > 0) { t--; int l, r; scanf("%d%d", &l, &r); printf("%d\n", query(l, r)); } } return 0; }
/********************************************************** * Locks the target by clearing the Debug Lock Word and * then performing a pin reset. **********************************************************/ void lockTarget(void) { uint32_t apId; printf("Locking target\n"); /* Clear Debug Lock Word */ writeWordToFlash(DEBUG_LOCK_WORD, 0); printf("Verifying that Debug Lock Word is cleared\n"); /* Verify that DLW is cleared */ uint32_t dlw = readMem(DEBUG_LOCK_WORD); if ( dlw != 0 ) { RAISE(SWD_ERROR_CLR_DLW_FAILED); } /* Perform a pin reset. After this the target should be locked. */ hardResetTarget(); delayMs(100); /* Verify that target is locked by reading the AAP ID */ initDp(); apId = readApId(); if ( apId == EFM32_AAP_ID ) { printf("Target successfully locked\n"); return; } else { RAISE(SWD_ERROR_LOCK_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); }