Hello Martin,
Further to what Ake has suggested, it is best to choose the first flash page for the storage of non-volatile data (assuming a single page provides sufficient storage). The program code would then commence at the second flash page. This means that the FLBPR register can be programmed to protect the program code, but leave the non-volatile data page unprotected.
I have previously used the following (absolute) assembly code, for a QY4 device, to erase the first flash page, and then program a word value to the first two positions in the page.
FLASH EQU $EE00 ; 4096 bytes total to $FDFF
PBLOCK EQU FLASH ; Non-volatile block for data
DATSIZE EQU 2
CODE EQU FLASH+$40 ; Start of program code
FLBPRVAL EQU {(FLASH-$C000)/$40+1} ; Unprotect first block
SPDVAL EQU 13 ; ~(3.2 * 4)
; ISP ROUTINES:
ERARNGE EQU $2806 ; Erase range of flash
PRGRNGE EQU $2809 ; Program range of flash
; RAM REGISTERS
; ISP DATA STRUCTURE:
ORG RAM+8
CTRLBYT DS 1 ; BIT-6 = Mass erase flag
CPUSPD DS 1 ; VALUE = 4*Fop (MHz)
LADDR DS 2 ; Last address of range (word value)
WADDR DS 2 ; Wireless address value
ORG CODE ; Start of program code
;***************************************************************
; ERASE FLASH PAGE & WRITE BLOCK
; On exit, CF = 1 if programmed data is verified, otherwise CF = 0
WRBLOCK: MOV #SPDVAL,CPUSPD
CLR CTRLBYT ; Page erase only
LDHX #PBLOCK
LDA #DATSIZE
PSHX
ADD 1,SP ; Add LS address
DECA ; Last position in block
TAX
STHX LADDR
PULX
JSR ERARNGE ; Erase range - disable interrupts
LDHX #PBLOCK
JSR PRGRNGE ; Program block
CLI ; Re-enable interrupts
RTS
;***************************************************************
ORG FLBPR ; Flash block protect register
DC.B FLBPRVAL ; First flash block unprotected
Regards,
Mac