cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 xl option bytes

Jeroen3
Senior
Posted on November 17, 2016 at 13:39

I'm unable to program the option bytes from user software. In an STM32F103RGT7.

Erase works, program does not.

Code:

// Lock and unlock

FLASH->CR = FLASH_CR_LOCK;

if( FLASH->CR & FLASH_CR_LOCK ){

  // Unlock fpec                

  FLASH->OPTKEYR = 0x45670123;

  FLASH->OPTKEYR = 0xCDEF89AB;

}

// Clear flags

FLASH->SR |= FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR;

// Set option bytes erase

FLASH->CR |= FLASH_CR_OPTER;  // Erase

// Start

FLASH->CR |= FLASH_CR_STRT;

__NOP();

while( FLASH->SR & FLASH_SR_BSY );

__NOP();

// Program 1

FLASH->CR = FLASH_CR_LOCK; 

if( FLASH->CR & FLASH_CR_LOCK ){

  // Unlock fpec   

  FLASH->KEYR = 0x45670123;

  FLASH->KEYR = 0xCDEF89AB;

  FLASH->OPTKEYR = 0x45670123;

  FLASH->OPTKEYR = 0xCDEF89AB;

}

FLASH->SR |= FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR;

FLASH->CR |= FLASH_CR_OPTPG; // Write

FLASH->CR |= FLASH_CR_STRT;

p = (__IO uint16_t *)0x1FFFF804;

*p = 0xAA; // <-- busfault

__NOP();

while( FLASH->SR & FLASH_SR_BSY );

__NOP();

As far as I know, I'm following the procedure from the programming manual.

I unlock OPTWRE, and write the keys to normal key register. Otherwise lock would never clear.

I set OPTPG.

I write a half-word.

I wait for BSY. 

But I can't get around the busfault.

What am I doing wrong?

- Using the std libs the code just halts and I get an iwdg reset after 10 seconds.

- I can load the code+options using keil and st link/ulink without problem.

- The ST Link ultity throws an error on writing of option bytes, but they are still programmed.

- Read protection is off.

0 REPLIES 0