My flash programming routines are based on example from the forums..
File.c
//==============================================================================
// Write a byte into the specified FLASH address
//==============================================================================
char flash_write(unsigned int address, unsigned char data)
{
unsigned char *pointer;
pointer = (char*) address;
FlashProg(pointer,data); // call the FLASH programming function
if (FSTAT_FACCERR) data=1; else data=0;
if (FSTAT_FPVIOL) data|=2;
return(data);
}
//==============================================================================
// Erase a sector of the FLASH memory
//==============================================================================
unsigned char flash_sector_erase(unsigned int address)
{
unsigned char *pointer, res;
pointer = (char*) address;
FlashErase(pointer);
if (FSTAT_FACCERR) res=1; else res=0;
if (FSTAT_FPVIOL) res|=2;
return(res);
}
File.asm
;*******************************************************************************
;
;*******************************************************************************
FlashErase:
psha ;adjust sp for DoOnStack entry
lda #(mFPVIOL+mFACCERR) ;mask
sta FSTAT ;abort any command and clear errors
lda #mPageErase ;mask pattern for page erase command
bsr DoOnStack ;finish command from stack-based sub
ais #1 ;deallocate data location from stack
rts
;*******************************************************************************
;
;*******************************************************************************
FlashProg:
psha ;temporarily save entry data
lda #(mFPVIOL+mFACCERR) ;mask
sta FSTAT ;abort any command and clear errors
lda #mByteProg ;mask pattern for byte prog command
bsr DoOnStack ;execute prog code from stack RAM
ais #1 ;deallocate data location from stack
rts
...