/********************************************************************* * (c) SEGGER Microcontroller GmbH & Co. KG * * The Embedded Experts * * www.segger.com * ********************************************************************** ---------------------------------------------------------------------- File : BeforeAndAfterFlashProg.JLinkScript Purpose : Example implementation for HandleBeforeFlashProg() and HandleAfterFlashProg() -------- END-OF-HEADER --------------------------------------------- */ /********************************************************************* * * Local functions * ********************************************************************** */ /********************************************************************* * * _FlushBuffer * * Function description * Reads 8 words from different offsets in order to flush the entire * buffer so we can be sure that all data are in the hardware now. */ void _FlushBuffer(void) { // // Disable the flash cache of the J-Link DLL temporarily because we have to make sure // that the DLL does not return cached data but accesses the hardware. // We make sure to re-enable the flash cache after buffer flush has been performed // JLINK_AllowFlashCacheState = 0; // // The buffer is a 4 x 16 word buffer but we have to read 2 * 4 = 8 buffer lines (each line is // 16 words in size) in order to make sure that we do not read a line which may be already buffered. // JLINK_MEM_ReadU32(0x80000000); JLINK_MEM_ReadU32(0x80001000); JLINK_MEM_ReadU32(0x80002000); JLINK_MEM_ReadU32(0x80003000); JLINK_MEM_ReadU32(0x80004000); JLINK_MEM_ReadU32(0x80005000); JLINK_MEM_ReadU32(0x80006000); JLINK_MEM_ReadU32(0x80007000); // // Enable flash cache again in order to make sure that // the J-Link DLL operates properly and as fast as possible // JLINK_AllowFlashCacheState = 1; } /********************************************************************* * * Global functions * ********************************************************************** */ /********************************************************************* * * HandleBeforeFlashProg * * Function description * This function is executed right before the J-Link DLL performs flash programming. */ void HandleBeforeFlashProg(void) { U32 v; JLINK_SYS_Report("****************************************************"); JLINK_SYS_Report("J-Link script: HandleBeforeFlashProg() J-Link script"); // // Buffer handling // This script is able to handle both cases: // 1) EMC buffer was enabled // 2) EMC buffer was NOT enabled // // To be able to restore the correct state after flash programming, the // actual EMC_STATICCONFIG value is stored in a helper variable, located // in the internal RAM (address 0x1000FFD0) of the LPC1788 device. Please // make sure to reserve this location in your linker file // v = JLINK_MEM_ReadU32(0x2009C200); // Read SFR: EMC_STATICCONFIG0 JLINK_MEM_WriteU32(0x1000FFD0, v); // Store the current status in the helper variable, located in the internal RAM JLINK_SYS_Report1(" EMC_STATICCONFIG0 read:", v); JLINK_SYS_Report(" Storing value in helper variable:"); JLINK_SYS_Report(" Check if buffer mode is enabled"); if (v & (1 << 19)) { // Buffer enabled? --> disable it JLINK_SYS_Report(" Buffer mode enabled --> disable it"); v &= 0xFFF7FFFF; JLINK_MEM_WriteU32(0x2009C200, v); } else { JLINK_SYS_Report1(" Buffer mode disabled", v); } JLINK_SYS_Report("****************************************************"); } /********************************************************************* * * HandleAfterFlashProg * * Function description * This function is executed right after the J-Link DLL performed flash programming */ void HandleAfterFlashProg(void) { U32 v; JLINK_SYS_Report("****************************************************"); JLINK_SYS_Report("J-Link script: HandleAfterFlashProg() J-Link script"); // // Buffer handling // This script is able to handle both cases: // 1) EMC buffer was enabled before flash programming // 2) EMC buffer was NOT enabled before flash programming // // In order to restore the correct value (EMC buffer enabled or disabled // we read back the EMC_STATICCONFIG value from the helper variable. The // helper variable contains the value before HandleBeforeFlashProg() may // have changed it. For further information regarding this, please refer to // HandleBeforeFlashProg(). // v = JLINK_MEM_ReadU32(0x1000FFD0); // Read back the helper variable, located in the internal RAM JLINK_SYS_Report1(" Helper variable read:", v); JLINK_SYS_Report(" Check if buffer mode was enabled before flash programming"); if (v & (1 << 19)) { // Buffer mode was enabled previously? --> enable it JLINK_SYS_Report(" Buffer mode was enabled before flash programming"); v = JLINK_MEM_ReadU32(0x2009C200); // Read SFR: EMC_STATICCONFIG0 if ((v & (1 << 19)) == 0) { JLINK_SYS_Report(" Buffer mode is actual disabled (expected) --> enable it again"); v |= (1 << 19); JLINK_MEM_WriteU32(0x2009C200, v); // // The LPC17xx series has a 4x16 words data buffer. In order to make sure that the correct // content (not the buffered one) is read by the MCU, we flush the entire buffer. // _FlushBuffer(); } else { JLINK_SYS_Report(" Buffer mode is enabled (unexpected) --> something went wrong"); JLINK_SYS_Report("************** ERROR ******************"); } } else { JLINK_SYS_Report(" Buffer mode was already disabled before flash programming"); } JLINK_SYS_Report("****************************************************"); }