;
; Desmonstration DESMO scripts
;
; Test Flash memory (29W320) attached to Cirrus EP7311/ARM
;

; Include standard definitions
file setup

; Setup function keys (happens when function is loaded)
key .1	FID		FID;
key .2	FPROG	FPROG 
key .3	CERASE	CERASE;
key .4	BERASE	BERASE;
key .5	FTEST	FTEST;

;
; Set the flash access mode to 16 or 32 bite
;
function FM
	if	#1 = 16
		w32 MEMCFG1	%10101100,00001100,00001111,00001100
	else
		if #1 = 32
			w32 MEMCFG1 %10101100,00001100,00001111,00001101
		else
			print "FM must be 16 or 32"
			stop
		end
	end
end

;
; Reset the flash back to READ mode
;
function FRESET
	fm 32
	w32 FLASH+(555*2) AA
	w32 FLASH+(2AA*2) 55
	w32 FLASH F0
end

;
; Test flash ID code
;
function FID
	print	"Reading Flash ID"
	fm 32
	w32	FLASH+(555*2) AA
	w32	FLASH+(2AA*2) 55
	w32	FLASH+(555*2) 90
	; Read with hardware address bits 1:0 at 0,0 (manufacturer code)
	r32 FLASH+0
	; Read with hardware address bits 1:0 at 0,1 (device code)
	r32	FLASH+2
	; Reset to read mode
	freset
end

;
; Program a byte into the flash
;
function FPROG
	print	"Programming FLASH location "{D}#1" with "{H}#2
	freset
	w32 FLASH+(555*2) AA
	w32 FLASH+(2AA*2) 55
	w32 FLASH+(555*2) A0
	w32 FLASH+(#1*2) #2
end

;
; Perform a CHIP erase
;
function CERASE
	print "Full CHIP erase - this takes QUITE a while!"
	freset
	w32	FLASH+(555*2) AA
	w32	FLASH+(2AA*2) 55
	w32	FLASH+(555*2) 80
	w32	FLASH+(555*2) AA
	w32 FLASH+(2AA*2) 55
	w32	FLASH+(555*2) 10
	while (r32 flash & FFFF) != FFFF
		print	"Waiting..."
		delay	1000
	end
	print "Erase complete!"
end

;
; Perform a BLOCK erase
;
function BERASE
	print	"Block erase"
	freset
	w32	FLASH+(555*2) AA
	w32	FLASH+(2AA*2) 55
	w32	FLASH+(555*2) 80
	w32	FLASH+(555*2) AA
	w32 FLASH+(2AA*2) 55
	w32	FLASH+(555*2) 30
	while (r32 flash & FFFF) != FFFF
		print	"Waiting..."
		delay	100
	end
end

;
; Complete test
;
function FTEST
	; Get the flash ID
	fid
	; erase the flash
	berase
	print "Display flash (erased)"
	fm 16
	r32	FLASH
	r32	FLASH+4
	; Program the flash
	fprog 0 1234
	fprog 1 5678
	fprog 2 9ABC
	fprog 3 DEF0
	print "Display flash (programmed)"
	fm 16
	r32 FLASH
	r32 FLASH+4
end
;
; Test memory from #1 to #2
function MTEST
	variable A	#1
	while A < #2
		w32 A 5AC0.A503
		A = A + 4
	end
	a = #1
	while A < #2
		if R32 A != 5AC0.A503
			print "Error (1) at" A
			exit
		end
		w32 A 03A5.C05A
		if R32 A != 03A5.C05A
			print "Error (2) at" A
			exit
		end
		A = A + 4
	end
end

;
; Since these print statements are not part of a FUNCTION,
; they will be performed when the FILE is loaded.
;
print "Use F1 - 'FID' to read flash ID"
print "Use F2 - 'FPROG location value' to program a flash location"
print "Use F3 - 'CERASE' to perform chip erase (takes 30-40 seconds)"
print "Use F4 - 'BERASE' to perform a block erase"
print "Use F5 - 'FTEST' to run full id/erase/program/read test"
print
