//***************************************************************************** // //! Set sector protection // //***************************************************************************** void FlashProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode) { uint32_t ui32SectorNumber; // // Check the arguments. // ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashSizeGet() - FlashSectorSizeGet())); ASSERT((ui32SectorAddress & (FlashSectorSizeGet() - 1)) == 00); if(ui32ProtectMode == FLASH_WRITE_PROTECT) { ui32SectorNumber = (ui32SectorAddress - FLASHMEM_BASE) / FlashSectorSizeGet(); HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_ENABLE; if(ui32SectorNumber <= 31) { HWREG(FLASH_BASE + FLASH_O_FSM_BSLE0) |= (1 << ui32SectorNumber); HWREG(FLASH_BASE + FLASH_O_FSM_BSLP0) |= (1 << ui32SectorNumber); } else if(ui32SectorNumber <= 63) { HWREG(FLASH_BASE + FLASH_O_FSM_BSLE1) |= (1 << (ui32SectorNumber & 0x1F)); HWREG(FLASH_BASE + FLASH_O_FSM_BSLP1) |= (1 << (ui32SectorNumber & 0x1F)); } HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_DISABLE; } }
//***************************************************************************** // //! Erases the upper flash page with use of ROM function //! //! This function erases the 2 kB upper page of the on-chip flash. After //! erasing, the page is filled with 0xFF bytes. A locked page cannot //! be erased. //! //! This function does not return until the flash page is erased or //! an error encountered. //! //! \return Returns 0 on success, -1 if erasing error is encountered //! or, -2 in case of illegal parameter use. // //***************************************************************************** int32_t FlashUpperPageErase(void) { uint32_t ui32UpperPageAddr; uint32_t ui32CurrentCacheMode; int32_t i32Stat; // 0 = pass, -1 = fail, -2 = wrong param i32Stat = 0; // // Find start address of upper flash page // ui32UpperPageAddr = FLASH_BASE + (FlashSizeGet() * 1024) - FLASH_ERASE_SIZE; // // Save current cache mode since the ROM function will change it. // ui32CurrentCacheMode = FlashCacheModeGet(); // // Erase the upper flash page by calling ROM function. // i32Stat = ROM_PageErase(ui32UpperPageAddr, FLASH_ERASE_SIZE); // // Restore cache mode. // FlashCacheModeSet(ui32CurrentCacheMode); // // Return status pass or fail. // return(i32Stat); }
//***************************************************************************** // //! Programs the upper page of the flash by use of ROM function //! //! \param pui32Data is a pointer to the data to be programmed. //! \param ui32Address is the starting address within the flash upper page to be //! programmed. Must be a multiple of four and within the flash upper page. //! \param ui32Count is the number of bytes to be programmed. Must be a multiple //! of four. //! //! This function programs a sequence of words into the on-chip flash. //! Programming each location consists of the result of an AND operation //! of the new data and the existing data; in other words, bits that contain //! 1 can remain 1 or be changed to 0, but bits that are 0 cannot be changed //! to 1. Therefore, a word can be programmed multiple times as long as these //! rules are followed; if a program operation attempts to change a 0 bit to //! a 1 bit, that bit will not have its value changed. //! //! Because the flash is programmed one word at a time, the starting address and //! byte count must both be multiples of four. The caller must //! verify the programmed contents, if such verification is required. //! //! This function does not return until the data is programmed or an //! error encountered. A locked flash page cannot be programmed. //! //! \return Returns 0 on success, -1 if a programming error is encountered //! or, -2 in case of illegal parameter use. // //***************************************************************************** int32_t FlashUpperPageProgram(uint32_t *pui32Data, uint32_t ui32Address, uint32_t ui32Count) { uint32_t ui32CurrentCacheMode; int32_t i32Stat; // 0 = pass, -1 = fail, -2 = wrong param i32Stat = 0; // Start out passing // // Check the arguments. // ASSERT(!(ui32Address < (FLASH_BASE + (FlashSizeGet() * 1024) - FLASH_ERASE_SIZE))); ASSERT(!((ui32Address + ui32Count) > (FLASH_BASE + (FlashSizeGet() * 1024)))); ASSERT(!(ui32Address & 3)); ASSERT(!(ui32Count & 3)); // // Save current cache mode since the ROM function will change it. // ui32CurrentCacheMode = FlashCacheModeGet(); // // Program flash by executing function in ROM. // i32Stat = ROM_ProgramFlash(pui32Data, ui32Address, ui32Count); // // Clear flash controller register bit set by ROM function. // HWREG(FLASH_CTRL_FCTL) &= (~FLASH_CTRL_FCTL_UPPER_PAGE_ACCESS); // // Restore cache mode. // FlashCacheModeSet(ui32CurrentCacheMode); // // Return status pass or fail. // return(i32Stat); }
//***************************************************************************** // //! Save sector protection to make it permanent // //***************************************************************************** uint32_t FlashProtectionSave(uint32_t ui32SectorAddress) { uint32_t ui32ErrorReturn; uint32_t ui32SectorNumber; uint32_t ui32CcfgSectorAddr; uint32_t ui32ProgBuf; ui32ErrorReturn = FAPI_STATUS_SUCCESS; // // Check the arguments. // ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashSizeGet() - FlashSectorSizeGet())); ASSERT((ui32SectorAddress & (FlashSectorSizeGet() - 1)) == 00); if(FlashProtectionGet(ui32SectorAddress) == FLASH_WRITE_PROTECT) { // // Find sector number for specified sector. // ui32SectorNumber = (ui32SectorAddress - FLASHMEM_BASE) / FlashSectorSizeGet(); ui32CcfgSectorAddr = FLASHMEM_BASE + FlashSizeGet() - FlashSectorSizeGet(); // // Adjust CCFG address to the 32-bit CCFG word holding the // protect-bit for the specified sector. // ui32CcfgSectorAddr += (((ui32SectorNumber >> 5) * 4) + CCFG_OFFSET_SECT_PROT); // // Find value to program by setting the protect-bit which // corresponds to specified sector number, to 0. // Leave other protect-bits unchanged. // ui32ProgBuf = (~(1 << (ui32SectorNumber & 0x1F))) & *(uint32_t *)ui32CcfgSectorAddr; ui32ErrorReturn = FlashProgram((uint8_t*)&ui32ProgBuf, ui32CcfgSectorAddr, CCFG_SIZE_SECT_PROT); }
//***************************************************************************** // //! Programs the flash main pages by use of ROM function //! //! \param pui32Data is a pointer to the data to be programmed. //! \param ui32Address is the starting address in flash to be programmed. Must //! be a multiple of four and within the flash main pages. //! \param ui32Count is the number of bytes to be programmed. Must be a multiple //! of four. //! //! This function programs a sequence of words into the on-chip flash. //! Programming each location consists of the result of an AND operation //! of the new data and the existing data; in other words, bits that contain //! 1 can remain 1 or be changed to 0, but bits that are 0 cannot be changed //! to 1. Therefore, a word can be programmed multiple times as long as these //! rules are followed; if a program operation attempts to change a 0 bit to //! a 1 bit, that bit will not have its value changed. //! //! Because the flash is programmed one word at a time, the starting address and //! byte count must both be multiples of four. The caller must //! verify the programmed contents, if verification is required. //! //! This function does not return until the data is programmed or an //! error encountered. Locked flash pages cannot be programmed. //! //! \return Returns 0 on success, -1 if a programming error is encountered //! or, -2 in case of illegal parameter use. // //***************************************************************************** int32_t FlashMainPageProgram(uint32_t *pui32Data, uint32_t ui32Address, uint32_t ui32Count) { uint32_t ui32CurrentCacheMode; int32_t i32Stat; // 0 = pass, -1 = fail, -2 = wrong param i32Stat = 0; // Start out passing // // Check the arguments. // ASSERT(!(ui32Address < FLASH_BASE)); ASSERT(!((ui32Address + ui32Count) > (FLASH_BASE + (FlashSizeGet() * 1024) - FLASH_ERASE_SIZE))); ASSERT(!(ui32Address & 3)); ASSERT(!(ui32Count & 3)); // // Save current cache mode since the ROM function will change it. // ui32CurrentCacheMode = FlashCacheModeGet(); // // Program flash by executing function in ROM. // i32Stat = ROM_ProgramFlash(pui32Data, ui32Address, ui32Count); // // Restore cache mode. // FlashCacheModeSet(ui32CurrentCacheMode); // // Return status pass or fail. // return(i32Stat); }
//***************************************************************************** // //! Get sector protection // //***************************************************************************** uint32_t FlashProtectionGet(uint32_t ui32SectorAddress) { uint32_t ui32SectorProtect; uint32_t ui32SectorNumber; // // Check the arguments. // ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashSizeGet() - FlashSectorSizeGet())); ASSERT((ui32SectorAddress & (FlashSectorSizeGet() - 1)) == 00); ui32SectorProtect = FLASH_NO_PROTECT; ui32SectorNumber = (ui32SectorAddress - FLASHMEM_BASE) / FlashSectorSizeGet(); if(ui32SectorNumber <= 31) { if((HWREG(FLASH_BASE + FLASH_O_FSM_BSLE0) & (1 << ui32SectorNumber)) && (HWREG(FLASH_BASE + FLASH_O_FSM_BSLP0) & (1 << ui32SectorNumber))) { ui32SectorProtect = FLASH_WRITE_PROTECT; } } else if(ui32SectorNumber <= 63) { if((HWREG(FLASH_BASE + FLASH_O_FSM_BSLE1) & (1 << (ui32SectorNumber & 0x1F))) && (HWREG(FLASH_BASE + FLASH_O_FSM_BSLP1) & (1 << (ui32SectorNumber & 0x1F)))) { ui32SectorProtect = FLASH_WRITE_PROTECT; } } return(ui32SectorProtect); }
//***************************************************************************** // //! Erases a flash main page with use of ROM function //! //! \param ui32Address is the start address of the flash main page to be erased. //! //! This function erases one 2 kB main page of the on-chip flash. After //! erasing, the page is filled with 0xFF bytes. Locked pages cannot be //! erased. The flash main pages do not include the upper page. //! //! This function does not return until the page is erased or an error //! encountered. //! //! \return Returns 0 on success, -1 if erasing error is encountered, //! or -2 in case of illegal parameter use. // //***************************************************************************** int32_t FlashMainPageErase(uint32_t ui32Address) { int32_t i32Stat; // 0 = pass, -1 = fail uint32_t ui32CurrentCacheMode; i32Stat = 0; // // Check the arguments. // ASSERT(!(ui32Address < FLASH_BASE)); ASSERT(!(ui32Address >= (FLASH_BASE + (FlashSizeGet() * 1024) - FLASH_ERASE_SIZE))); ASSERT(!(ui32Address & (FLASH_ERASE_SIZE - 1))); // // Save current cache mode since the ROM function will change it. // ui32CurrentCacheMode = FlashCacheModeGet(); // // Erase the specified flash main page by calling ROM function. // i32Stat = ROM_PageErase(ui32Address, FLASH_ERASE_SIZE); // // Restore cache mode. // FlashCacheModeSet(ui32CurrentCacheMode); // // Return status pass or fail. // return(i32Stat); }
//***************************************************************************** // //! Get the size of the flash // //***************************************************************************** uint32_t FlashsafeSizeGet(void) { return(FlashSizeGet()); }