I am trying to initialize a section of EEPROM address from 0x40A0 to 0x40B8 in STM8AF5288 MCU. Between I am not able to complete this initialization process & end up getting MCU Reset. Attaching the assembly code that does this action
BSET _FLASH_CR1,#0
BTJF _FLASH_IAPSR,#3,unlocked ; check data memory EEPROM write is unlocked
MOV _FLASH_DUKR,#0xae
MOV _FLASH_DUKR,#0x56
unlocked:
LDW X,#0x47FF ; p = EEPROM_END
LD A,(X)
CP A,#0xAA ; when initialization is complete the end of EEPROM address range contains 0xAA
JRNE PrepInit
JRPL InitComplete
PrepInit:
LDW X,#0x40AD ; load initialization range start address
ContinueInit:
CLR A
LD (X),A ; clear EEPROM location
NOP
writing:
BTJF _FLASH_IAPSR,#2,writing ; check EOP is true to detect EEPROM byte update complete
INCW X
CPW X,#0x40B8 ; check if we had reached the end of initialization range
JRC ContinueInit
LDW X,#0x47FF ; p = EEPROM_END
LD A,#0xAA ; update showing initialization completed
LD (X),A
NOP
NOP
BSET _FLASH_IAPSR,#3 ; lock data eeprom write access
InitComplete:
callf f_main ; execute main
i hope the procedure is correct, please advice if something is wrong in this code. This is done before invoking main function.
Couldn't follow the FLASH_IAPSR changes in this windows as well. It shows "intrusive read"

Thanks