//***************************************************************************** // //! 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; } }
//***************************************************************************** // //! 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); }
//***************************************************************************** // //! 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); }
//***************************************************************************** // //! Get size of a flash sector in no of bytes // //***************************************************************************** uint32_t FlashsafeSectorSizeGet(void) { return(FlashSectorSizeGet()); }