cancel
Showing results for 
Search instead for 
Did you mean: 

How to write option bytes in assembly language for stm8 mcu in stvd

saran raj
Associate III
Posted on October 25, 2017 at 07:04

How to write option bytes in assembly language for stm8 mcu in stvd developer using cosmic compiler.

I have to set Read out protection without using the stvp programmer.

3 REPLIES 3
Thomas G
Associate II
Posted on October 25, 2017 at 07:55

Hi!

If you translate this bit of Forth into assembly, then here is how to do it.

Note that '\ ..' and '( .. )' are comments. Data is passed through the data stack, and you can probably keep it in registers when coding it in assembly.

\ STM8S option setting words

\ refer to github.com/TG9541/stm8ef/blob/master/LICENSE.md

\ store char c to (a), inverted value to (a+1)

: CN! ( c a -- )

  2DUP C! SWAP NOT SWAP 1+ C! ;

\ unlock write protection, store option byte

: OPT! ( c a -- )

  FLASH_CR2 DUP C@ $80 OR SWAP CN! \ unlock option bytes

  ULOCK CN! LOCK \ unlock EEPROM, write c to (a) ;

\\ ULOCK and LOCK from forth.asm:

; LOCK ( -- )

; Lock EEPROM (STM8S)

   HEADER LOCK 'LOCK'

LOCK:

   BRES FLASH_IAPSR,#3

   RET

   HEADER ULOCK 'ULOCK'

ULOCK:

   MOV FLASH_DUKR,#0xAE

   MOV FLASH_DUKR,#0x56

1$: BTJF FLASH_IAPSR,#3,1$ ; PM0051 4.1 requires polling bit3=1 before writing

   RET

Posted on October 25, 2017 at 08:32

will this work ?

org 04800h

LOCKBYTE:dc.b 0AAh; Read out Bytes

OPT1:dc.b 000h

NOPT1:dc.b 0ffh

OPT2:dc.b 081h

NOPT2:dc.b 07eh

OPT3:dc.b 000h

NOPT3:dc.b 0ffh

OPT4:dc.b 000h

NOPT4:dc.b 0ffh

OPT5:dc.b 000h

NOPT5:dc.b 0ffh

OPT6:dc.b 000h

NOPT6:dc.b 0ffh

OPT7:dc.b 000h

NOPT7:dc.b 0ffh

end

Thomas G
Associate II
Posted on October 25, 2017 at 19:20

No, I don't think that this will work. You can't program the option bytes without unlocking the EEPROM first, and that's something a flash tool won't do while flashing the device.

You need to solve it either it on the tool chain level (using a programmer like ST-Link), or using IAP (in application programming) with the procedure laid out in

http://www.st.com/content/ccc/resource/technical/document/programming_manual/0f/82/71/32/87/2a/46/f2/CD00191343.pdf/files/CD00191343.pdf/jcr:content/translations/en.CD00191343.pdf

Chapter 4.1. That's what the code above does. It's likely that there is also a STM8 library function to do it.