AnsweredAssumed Answered

Option Byte programming problem

Question asked by de_beun.arthur on Aug 30, 2013
Latest reply on Aug 30, 2013 by Clive One
I'm struggling to get the Option Byte programming to work from code. There is probably an obvious mistake here that I fail to see. I'm using an STM32373C-Eval board and the Firmware Libraries version 1.0.0 with the GCC compiler (launchpad).
It appears that I can successfully unlock the FPEC (FLASH_CR = 0) and Option Bytes (FLASH_CR = 0x200). Then I call the erase function. This function says it returns successfully (4 = FLASH_COMPLETE), but the Option Bytes are not actually erased. (In the code below the actual programming of the Option Byte is commented out, since that's the second step and doesn't work either.)
I'm using the ST-Link utility to check the value of the Option Bytes, before and after.
The Status Register (FLASH_SR) always has the PGERR bit set, which I'm not able to clear. Although I haven't read anywhere that clearing this error bit is a necessary condition for flash programming to work properly, it may be part of the problem.
This is my code, including debug printf statements:
printf("FLASH_CR = 0x%X\n\r", FLASH->CR);
printf("FLASH_SR = 0x%X\n\r", FLASH_GetStatus());
FLASH_Unlock();
printf("FLASH_CR = 0x%X\n\r", FLASH->CR);
printf("FLASH_SR = 0x%X\n\r", FLASH_GetStatus());
FLASH_ClearFlag(FLASH_SR_PGERR);
printf("FLASH_CR = 0x%X\n\r", FLASH->CR);
printf("FLASH_SR = 0x%X\n\r", FLASH_GetStatus());
FLASH_OB_Unlock();
FLASH_ClearFlag(FLASH_SR_PGERR);
printf("FLASH_CR = 0x%X\n\r", FLASH->CR);
printf("FLASH_SR = 0x%X\n\r", FLASH_GetStatus());
temp = FLASH_OB_Erase();
printf("FLASH_OB_Erase = %x\n\r", temp);
printf("FLASH_CR = 0x%X\n\r", FLASH->CR);
printf("FLASH_SR = 0x%X\n\r", FLASH_GetStatus());
// temp = FLASH_OB_WriteUser(USER_CONFIGURATION_OPTION_BYTE);
// printf("FLASH_OB_WriteUser = %x\n\r", temp);
FLASH_OB_Lock();
FLASH_Lock();
printf("FLASH_CR = 0x%X\n\r", FLASH->CR);
printf("FLASH_SR = 0x%X\n\r", FLASH_GetStatus());
This is the resulting output:
FLASH_CR = 0x80
FLASH_SR = 0x4

FLASH_CR = 0x0
FLASH_SR = 0x4

FLASH_CR = 0x0
FLASH_SR = 0x4

FLASH_CR = 0x200
FLASH_SR = 0x4

FLASH_OB_Erase = 4

FLASH_CR = 0x200
FLASH_SR = 0x4

FLASH_CR = 0x80
FLASH_SR = 0x4
Can anyone show me what the problem is? What am I missing here?

Thanks
Arthur

Outcomes