cancel
Showing results for 
Search instead for 
Did you mean: 

Option bytes complementary error with manual write

daniel.benes
Associate

Hello.

I'm trying to set Option bytes on STM8s005k6 from main code, but still I'm getting error in complementary bytes (when I try to rad out it back in STVP) and MCU hangs.

I'm using SDCC compiler with this code:

	// Set option bytes because of buzzer
	if(OPT->OPT2 != 0x80 || OPT->OPT3 != 0x08){
		FLASH->CR2 |= FLASH_CR2_OPT; //unlock option bytes for writing
 		FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NOPT);
		while (!(FLASH->IAPSR & FLASH_IAPSR_DUL));
 
		OPT->OPT2 = 0x80; // enable LSI clock source
		OPT->NOPT2 = 0x7f;
		OPT->OPT3 = 0x08; // set PD4 as alternative buzzer output
		OPT->NOPT3 = 0xf7;
		while (!(FLASH->IAPSR & FLASH_IAPSR_EOP)); // wait for write finish
 
		FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);	//lock back
  		FLASH->NCR2 |= FLASH_NCR2_NOPT;
	}

If I set it through graphic STVP and read OPT2 and OPT3 registers, they have exactly these values. But from code it doesn't accept :\

Thank you for any tips.

2 REPLIES 2
Kumar B
Associate II
 
Cristian Gyorgy
Senior III

Hello Daniel!

Ofcourse the MCU hangs. On line 5, in the while loop you wait for the DUL flag to be set, but you never write the unlocking keys to DUKR register.

You need to write the 2 hardware keys in FLASH_DUKR register, than you can check if DUL flag is set - please check the Reference Manual for more details.