hi,
Following example is for DP512. The principle is the same. Just placement and sizes are different.
You can check also principle in the C code project- attached.
Best regards, Ladislav
;_______________________________________________________________________________
; - Example present erase sector and write word to the EEPROM
; - Before write it checks:
; * alignment (2B)
; * whether sector is erased
; - Before sector erase it checks:
; * alignment (4B)
; - Variable err_code is used as a error return code
;
;_______________________________________________________________________________
;The Answer to the Ultimate Question of Life, the Universe, and Everything is: 42
;(Douglas Adams - The Hitchhiker's Guide to the Galaxy)
;_______________________________________________________________________________
; export symbols
XDEF Entry ; export 'Entry' symbol
ABSENTRY Entry ; for absolute assembly: mark this as application entry point
; include derivative specific macros
INCLUDE 'derivative.inc'
;_______________________________________________________________________________
; definitions
;_______________________________________________________________________________
ROMStart EQU $C000 ; absolute address to place my code/constant data
ERR_OK: EQU $0 ; OK
ERR_RANGE: EQU $1 ; Parameter out of range. (not alligned sector 4B; data 2B))
ERR_BUSY: EQU $2 ; Device is busy.
ERR_NOTAVAIL: EQU $3 ; Requested value or method not available.
ERR_NOTERASED: EQU $4 ; Requested address is not erased
;_______________________________________________________________________________
; variable/data section
;_______________________________________________________________________________
ORG RAMStart
;_______________________________________________________________________________
EE_address DS.W 1 ; address for sector erase or data write
EE_data DS.W 1 ; data to be written
err_code DS.B 1 ; return value from flash erase/write functions
;_______________________________________________________________________________
; code section
;_______________________________________________________________________________
ORG ROMStart
;_______________________________________________________________________________
; main loop
;_______________________________________________________________________________
Entry:
;--- memory mapping ------------------
;- reset default memory mapping is: --
;INITRG = 0x00; Set the Reg map position 0x0000~0x03FF
;INITEE = 0x01; Set the EEPROM map position 0x0000~0x0FFF, EEON=1 => visible 0x0400~0x0FFF
;INITRM = 0x09; Set the RAM map position 0x0800~0x3FFF
MOVB #$01,INITRM ; INITRM = 0x01; Set the RAM map position 0x0800~0x3FFF
MOVB #$41,INITEE ; INITEE = 0x41; Set the EEPROM map position 0x4000~0x4FFF, EEON=1
MOVB #$0 ,INITRG ; INITRG = 0x00; Set the Reg map position 0x0000~0x03FF
NOP; ; recommended
;--- Initialize stack ----------------
LDS #$4000 ; initialize stack on the top of RAM
;--- EEPROM clock---------------------
; MOVB #$29,ECLKDIV ; ECLKDIV = 0x29, eeprom clock prescaler; OSCCLK = 8MHz BUSCLK=4MHz
MOVB #$4A,ECLKDIV ; ECLKDIV = 0x4A, eeprom clock prescaler; OSCCLK = 16MHz BUSCLK=8MHz
loop:
;--- Erase sector --------------------
MOVW #$4000,EE_address ; prepare address for write function
JSR EraseSector ; erase sector at address $4000
;--- Write word to eeprom ------------
MOVW #$4000,EE_address ; prepare address for write function
MOVW #$1234,EE_data ; prepre data for write function
JSR WriteWord ; write word to the eeprom without checking address and EEPROM erased
;--- check error code and written data
;...
;...
;...
;--- Write word to eeprom ------------
MOVW #$4002,EE_address ; prepare address for write function
MOVW #$5678,EE_data ; prepre data for write function
JSR WriteWord ; write word to the eeprom without checking address and EEPROM erased
;--- check error code and written data
;...
;...
;...
;--- loop forever---------------------
JMP loop ; endless loop
;_______________________________________________________________________________
; WriteWord
; inputs: EE_address
; EE_data
; output: err_code
;_______________________________________________________________________________
WriteWord:
PSHD
PSHX
LDD EE_address ; check alignment (4B)
LDX #$2
IDIV
CPD #$0 ; if != $0 => error
BEQ WriteWord_LBL6
MOVB #ERR_RANGE,err_code ;return ERR_RANGE;
BRA WriteWord_LBL2
WriteWord_LBL6: ; check whether word is errased
LDX EE_address ;*(volatile word *) AddrRow = Data16; // Array address and program data
LDD 0,X
CPD #$FFFF ; if != $FFFF => error
BEQ WriteWord_LBL5
MOVB #ERR_NOTERASED,err_code ;return ERR_BUSY; // If yes then error
BRA WriteWord_LBL2
WriteWord_LBL5:
BRSET ESTAT,#128,WriteWord_LBL1 ;if (ESTAT_CBEIF == 0) // Is command buffer full ?
MOVB #ERR_BUSY,err_code ;return ERR_BUSY; // If yes then error
BRA WriteWord_LBL2
WriteWord_LBL1:
MOVB #$30,ESTAT ;ESTAT = 0x30; // Clear error flags
LDX EE_address ;*(volatile word *) AddrRow = Data16; // Array address and program data
LDD EE_data
STD 0,X
MOVB #$20,ECMD ;ECMD = 0x20; // Word program command
BSET ESTAT,#$80 ;ESTAT_CBEIF = 1; // Clear flag command buffer empty
BRSET ESTAT,#$20,WriteWord_LBL3 ;if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) // Is protection violation or acces error detected ?
BRCLR ESTAT,#$10,WriteWord_LBL4
WriteWord_LBL3:
MOVB #ERR_NOTAVAIL,err_code ;return ERR_NOTAVAIL; // If yes then error
BRA WriteWord_LBL2
WriteWord_LBL4:
LDAB ESTAT ;while (EEPROM_BUSY); // Wait for command completition
ANDB #192
CMPB #192
BNE WriteWord_LBL4
MOVB ERR_OK,err_code ;return ERR_OK;
WriteWord_LBL2:
PULX
PULD
RTS
;_______________________________________________________________________________
; EraseSector
; inputs: EE_address
; output: err_code
;_______________________________________________________________________________
EraseSector:
PSHD
PSHX
LDD EE_address ; check alignment (4B)
LDX #$4
IDIV
CPD #$0 ; if != $0 => error
BEQ EraseSector_LBL5
MOVB #ERR_RANGE,err_code ;return ERR_RANGE;
BRA EraseSector_LBL2
EraseSector_LBL5:
BRSET ESTAT,#128,EraseSector_LBL1;if (ESTAT_CBEIF == 0) // Is command buffer full ?
MOVB #ERR_BUSY,err_code ;return ERR_BUSY; // If yes then error
BRA EraseSector_LBL2
EraseSector_LBL1:
MOVB #$30,ESTAT ;ESTAT = 0x30; // Clear error flags
LDX EE_address ;*(volatile word *) AddrRow = Data16; // Array address and program data
LDD #$FFFF ; dummy data
STD 0,X
MOVB #$40,ECMD ;ECMD = 0x20; // Word program command
BSET ESTAT,#$80 ;ESTAT_CBEIF = 1; // Clear flag command buffer empty
BRSET ESTAT,#$20,EraseSector_LBL3 ;if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) // Is protection violation or acces error detected ?
BRCLR ESTAT,#$10,EraseSector_LBL4
EraseSector_LBL3:
MOVB #ERR_NOTAVAIL,err_code ;return ERR_NOTAVAIL; // If yes then error
BRA EraseSector_LBL2
EraseSector_LBL4:
LDAB ESTAT ;while (EEPROM_BUSY); // Wait for command completition
ANDB #192
CMPB #192
BNE EraseSector_LBL4
MOVB ERR_OK,err_code ;return ERR_OK;
EraseSector_LBL2:
PULX
PULD
RTS
;_______________________________________________________________________________
;**************************************************************
;* Interrupt Vectors *
;**************************************************************
ORG $FFFE
DC.W Entry ; Reset Vector