2016-11-17 04:39 AM
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 flagsFLASH->SR |= FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR;// Set option bytes eraseFLASH->CR |= FLASH_CR_OPTER; // Erase// StartFLASH->CR |= FLASH_CR_STRT;__NOP();while( FLASH->SR & FLASH_SR_BSY );__NOP();// Program 1FLASH->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; // WriteFLASH->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.