cancel
Showing results for 
Search instead for 
Did you mean: 

Flash write protection

philippe239955_stm1
Associate II
Posted on September 24, 2009 at 18:12

Flash write protection

4 REPLIES 4
philippe239955_stm1
Associate II
Posted on May 17, 2011 at 13:13

Hello,

I am currently exploring the read/write capacities of a STM32F103.

Basically, I am trying to write a value at a desired memory address and then write protect it but there seems to be a probleme with the FLASH_GetWriteProtectionOptionByte() function as it always return 0xFFFFFFFF.

This is the code I am using :

/******************************************************/

FLASH_Unlock();

FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR);

WRPR_Value = FLASH_GetWriteProtectionOptionByte();

ProtectedPages = WRPR_Value & 0x000000C0;

if (ProtectedPages == 0x00)

{/* Pages are write protected */

/* Disable the write protection */

FLASHStatus = FLASH_EraseOptionBytes();

/* Generate System Reset to load the new option byte values */

NVIC_GenerateSystemReset();

}

if (ProtectedPages != 0x00)

{/* Pages not write protected */

/* Enable the pages write protection */

FLASHStatus = FLASH_EnableWriteProtection(FLASH_WRProt_Pages24to27 |FLASH_WRProt_Pages28to31);

/* Generate System Reset to load the new option byte values */

NVIC_GenerateSystemReset();

}

/******************************************************/

The thing is that, on the first time, the logic seems correct, the WRPR_Value returns no write protection (0xFFFFFFF) and enter the condition that enables it. However, after the reset, it does exactly the same thing resulting in an infinite loop.

I saw that there was a similar problem here :

http://www.st.com/mcu/forums-cat-8383-23.html

but the description of the solution did not really helped.

I also followed the PM0042 documents with no success.

HSI is enabled. Also I am not able to write in memory using FLASH_ProgramWord(Address, Data)

I tried to clear all the option bytes and erase everything to no end. The contradictory results I'm getting are quite confusing.

Has anyone encountered such a problem? I am getting low on creativity to solve this one.

Thanks

Philippe Michaud-Côté

philippe239955_stm1
Associate II
Posted on May 17, 2011 at 13:13

A good night of sleep helped I guess.

For a reason that I have yet to understand, the FLASH_GetWriteProtectionOptionByte() doesn't return 0xFFFFFFFF after a reset anymore.

Also, I dicovered that the FLASH_EnableWriteProtection() can only be called once in my case. If I call it twice, only the first one will have an effect on the OptionBytes. (The second I would understand but why the first one?) I also need to erase the option bytes before calling FLASH_EnableWriteProtection(), otherwise it just won't work.

However, a new problem appeared. If I try to write a particular value to the flash with FLASH_ProgramHalfWord(Address, Data) or FLASH_ProgramWord(Address, Data) an Hard Fault occur. Whether the flash is write protected or not does not change the fault. From what I can gather with the documentation in PM0056 and the registers at the moment the fault occurs, the fault is escalated from an Imprecise Data Bus Error. The error happens on the moment ''FLASH_WaitForLastOperation()'' is called and the program doesn't seems to enter the function.

I will probably get around the hard fault one way or another, but does anyone know why, if I call FLASH_EnableWriteProtection() more than once, only the first call applies? (I guess it is linked with the fact that I must erase the option bytes before aplying the write protection.)

regards,

Philippe Michaud-Côté

daleyue
Associate II
Posted on May 17, 2011 at 13:13

first, the statements after your first system reset will never be excuted.

2nd, if the read-protection is enabled(might be the result of erase the option bytes), you will not be able to debug, and come across hard fault.

hope it helps.

tomas23
Associate II
Posted on May 17, 2011 at 13:13

Isn't it recommended to reset the device if changing the RO/RW protections to apply the changes to the FLASH controller? Pls check in Flash programming manual.