Flash

To access flash you need to use the LDC/LDCI instructions.

Reading from flash

The way you read from flash is very similar to the way you read from registers, but instead of using ld or ldx instructions you need to use LDC. Example:

ld R2,#00h; set RR2 to 0002h
ld R3,#02h
ldc R0,@RR2;load high byte
incw RR2;go to next address
ldc R1,@RR2;load low byte

In this example the cpu loads the reset vector into register pair RR0. The reset vector is stored at address 0002h in flash.

Writing to flash

Flash is divided into 512 bytes long pages. If your microcontroller has 64KB of flash you have 128 pages in total. You can only write to flash once you unlocked the flash controller and selected a page. This is a safety measure that prevents your program from accidentally overwriting anything. Once you unlock a page you can write to it using LDC. Before you write to flash you need to erase the entire page. That is just the nature of flash. Once erased all bytes are set to FFh(255). Then write to it using LDC src,dst. Example:

;; in your initialization call init_flash to set up the flash controller
init:
	call init_flash
main:
	;Select page 20
	ld R0,#20
	call F_unlock
	;Write 00 to 2800h in flash
	ld R2,28h
	ld R3,00h
	ldc @RR2,#00h
	call F_lock

You will need to include this code in your project:

;; Routine: init_flash
;; Sets up the flash controller. SYSFREQ should be defined in your code.
;; Put this somewhere at the beginning of your program before you start using any of the other routines listed below.
init_flash
	ldx FFREQH,#HIGH(SYSFREQ/1000)
	ldx FFREQL,#LOW(SYSFREQ/1000)
	ret
	
;; Routine: F_erase:
;; Erase a page (sets all values to FF)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; R0 = page number
F_erase:
	call F_unlock
	ldx FCTL,#95h
	ret

;; Routine: F_unlock
;; Unlocks a page for writing. You cannot unlock multiple pages at once.
;; Opening another page will simply close the last one.	
F_unlock:
	ldx FCTL,#00h
	ldx FPS,R0
	ldx FCTL,#73h
	ldx FCTL,#8Ch
	ldx FPS,R0
	ret

;; Routine: F_lock:
;; Lock the page for writing. Do this after you're done writing to the page.
F_lock:
	ldx FCTL,#00h
	ret

Please submit corrections, suggestions, and new documentation here: Submit a ticket
← Return to Reference Index
This is not an official Zilog website. Zilog nor 8times8 are liable for any damage done to equipment. Tutorial and reference copyright ©8times8 Creative Commons CC-0 License (public domain.) This reference uses content from Zilog's technical datasheets. Fair use only. Datasheets copyright © 2013 Zilog®, Inc. Z8, Z8 Encore!, Z8 Encore! XP and Z8 Encore! MC are trademarks or registered trademarks of Zilog, Inc. Read Zilog's terms at zilog.com

Website design by Koen van Vliet. Hosted by sourceforge.net