Skip to main content
Visitor II
July 25, 2018
Question

I'm using the STM8S003 and I've to change the default value of the option byte AFR0. This operation is easy using a programmer but I'd like to insert this procedure in the source code. Is it possible? Some examples ? Thanks,

  • July 25, 2018
  • 2 replies
  • 925 views

Hi everyone,

I'm using the STM8S003 and I need to use the alternate function of PC5 so I've to change the default value of the option byte AFR0.

This operation is easy using a programmer (ex. STVP) but I'd like to insert this procedure in the source code. I've created this small routine for doing it: 

if(FLASH_ReadOptionByte(0x4803)!=0x01) 

FLASH_Unlock(FLASH_MEMTYPE_DATA); 

FLASH_EraseOptionByte (0x4803);

FLASH_ProgramOptionByte(0x4803, 0x01);  

FLASH_Lock(FLASH_MEMTYPE_DATA); 

It seems to work correctly but I'd like to have your opinion about it. 

Thanks,

Alan 

    This topic has been closed for replies.

    2 replies

    Graduate II
    August 10, 2018

    @ST Community​ anyone from team STM8?

    Visitor II
    August 22, 2018

    Hi, I'm using the STM8S001J.

    I don't use libraries, but reading the datasheet I discovered that writing the option bytes is very similar to writing Eeprom, so I realized a couple of simple functions. You can compare it with yours to verify if it's the same thing. This is the function I wrote to program an option byte:

    void OptionWrite(char *addr, unsigned char data) // write a byte to OPTIONS (read=EarRead)
    {
    	FLASH_DUKR = 0xAE; // unlock the EEprom
    	FLASH_DUKR = 0x56; //
     FLASH_CR2 |= 0x80; // unlock the OPTION (set bit OPT)
     FLASH_NCR2 &= 0x7F; // (clear bit NOPT)
    	*addr = data; // write data
    	while((FLASH_IAPSR & 0x04) == 0); // wait for EOP (end-of-programming) set
    	FLASH_IAPSR = 0x00; // Write protect the EEPROM clearing DUL bit
    }

    and this is to read from it (it's the same as for the Eeprom):

    unsigned char EarRead(char *addr) // read a byte from EEprom
    {
    BYTE data;
     
    	data = *addr; // read data
    	return data;
    }

    Finally, this is an example of how to use it:

    	mc = EarRead((char *)0x4803); // read option byte OPT2 from EEprom
    	if(mc != 0x03) // if option programming different from requested one
    		{
    		OptionWrite((char *)0x4803, 0x03); // write OPT2 (0x4803)
    		OptionWrite((char *)0x4804, 0xFC); // write NOPT2 (0x4804)
    		}

    Every option byte must be written in direct and complemented form. In this case I wanted the OPT2 = 0x03, so I wrote 0x03 in OPT2 and 0xFC in NOPT2.

    I used the OptionWrite just as an experiment, so it's NOT fully tested, but it worked.

    Sorry for confused tabs/indent in listings, but it's impossible to edit them after a copy/paste in the "code" box.

    Any confirmation by the STM8 gurus will be highly appreciated. Thanks.