2010-07-01 11:49 AM
STM32 FLASH Page Write Protection
#stm32 #stm322011-05-17 04:57 AM
Thanks for the suggestion. I found an STMicro example provided with my IAR install that seems pretty close to what I need. My version of the code is shown below. The problem I'm having now is that the FLASH_EnableWriteProtection function is failing, with a status of FLASH_ERROR_PG, when trying to write protect the specific pages. Not sure if there is something else I need to configure?
// Unlock the Flash Program Erase controller
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP| FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
// Get pages write protection status
unsigned int WRPR_Value = FLASH_GetWriteProtectionOptionByte();
unsigned int ProtectedPages = WRPR_Value & 0x0000000F; // (FLASH_WRProt_Pages0to1 | FLASH_WRProt_Pages2to3 |
// FLASH_WRProt_Pages4to5 | FLASH_WRProt_Pages6to7)
// Pages are not write protected
if (ProtectedPages != 0x00)
{
// protect bootloader FLASH area
FLASH_EnableWriteProtection(FLASH_WRProt_Pages0to1 | FLASH_WRProt_Pages2to3 |
FLASH_WRProt_Pages4to5 | FLASH_WRProt_Pages6to7);
// Generate System Reset to load the new option byte values
NVIC_GenerateSystemReset();
}
FLASH_Lock();
2011-05-17 04:57 AM
Try putting
FLASH_Unlock(); before your line and FLASH_Lock(); after it. The flash controller prevents updates unless enabled. The write protect bits are in flash so...2011-05-17 04:57 AM
The flash write-protect bits are stored in flash and hence need to be erased before writing if there's any 0->1 transitions.
Those bits are in the option bytes area of flash and this has its own erase function FLASH_EraseOptionBytes() The trouble with that is that it erases ALL option bytes so you have to write your own function to save the existing option bytes, program the write-protect bits and then reprogram the other option bytes. I suggest you read the manual (all of it).2011-05-17 04:57 AM
The flash write-protect bits are stored in flash and hence need to be erased before writing if there's any 0->1 transitions.
It is more worse. Every 16 bit of the option byte area can only be written one time after an erase command to the option byte flash. The option bytes have a ''xor check sum'' in each following byte: option byte check sum option byte check sum ... Even if the flash could change single bits (STMs flash in STM32f1xx can NOT), the check sums would prevent.2011-05-17 04:57 AM
Thanks for the suggestions. Here is the code I ended up with. It seems to be working pretty well.
// unlock the Flash Program Erase Controller (FPEC)
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP| FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
// get pages write protection status
unsigned int WRPR_Value = FLASH_GetWriteProtectionOptionByte();
unsigned int ProtectedPages = WRPR_Value & (FLASH_WRProt_Pages0to1 | FLASH_WRProt_Pages2to3 |
FLASH_WRProt_Pages4to5 | FLASH_WRProt_Pages6to7);
// pages are not write protected
if(ProtectedPages != 0x00)
{
// erase option bytes (must do this before writing option bytes)
FLASH_EraseOptionBytes();
// read protection needs to happen before write protection, since this
// function erases all option bytes
FLASH_ReadOutProtection(ENABLE);
// program user option bytes
FLASH_UserOptionByteConfig(OB_IWDG_SW, OB_STOP_NoRST, OB_STDBY_NoRST);
// protect bootloader FLASH area
FLASH_EnableWriteProtection(FLASH_WRProt_Pages0to1 | FLASH_WRProt_Pages2to3 |
FLASH_WRProt_Pages4to5 | FLASH_WRProt_Pages6to7);
// generate System Reset to load the new option byte values
NVIC_GenerateSystemReset();
}
// lock the Flash Program Erase Controller (FPEC)
FLASH_Lock();