/******************************************************************************/
//
// Name: 	NAND FLASH interface with ADSP-21161
//
/******************************************************************************

(C) Copyright 2005 - Analog Devices, Inc.  All rights reserved.

File Name:		BlockProgram.asm

Date Modified:	20/06/05 - AV	-	Rev 1.0

Software:       VisualDSP++4.0

Purpose:		Demostrate NAND FLASH Interface
				
*******************************************************************************/

#include <def21161.h>

.global FLASH_PROGRAM_Command;


.extern CLE_delay_func;
.extern ALE_delay_func;
.extern POLL_delay_func;
.extern POLL_delayErase_func;
.extern AFTER_RD_WR_delay_func;
.extern Delay_Before_Return_func;

.section/pm seg_pmco;
//PAGE PROGRAM Command	
	FLASH_PROGRAM_Command:
	bit clr FLAGS FLG3;  //enable chip - FLASH CS# asserted here
	bit set FLAGS FLG1;  //Drive CLE "HI" for the command phase

	//First byte  - command "0x80"
	r0 = 0x80;
	dm(0x00200000) = r0; //write to bank 3 address space - FLASH is interfaced to bank3	
	
	//Drive CLE "LO"
	CALL CLE_delay_func;//call CLE delay
	
	bit clr FLAGS FLG1;
	//Drive ALE "HI" - address phase
	bit set FLAGS FLG0;
	//Write "column", "rox_1_2_3" address
	
	dm(0x00200000) = r12; //"0"th column - 0th byte
	dm(0x00200000) = r13; //"0"th row 
	dm(0x00200000) = r14; //"1"st row
	dm(0x00200000) = r15; //"2"nd row
	
	//Drive ALE "LO"
	CALL ALE_delay_func;//call ALE delay
	bit clr FLAGS FLG0;
	
	//write data DN, DN+1......DN+527
	lcntr = 528, do page_write until lce;
	r1 = dm(i1,m0);

	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
	page_write: dm(0x00200000) = r1;
	
	CALL AFTER_RD_WR_delay_func;
	
	//Give 0x10 command

	bit set FLAGS FLG1; //Drive CLE "HI"
	//Command "0x10"
	r0 = 0x10;
	dm(0x00200000) = r0;
	//Drive CLE "LO"
	CALL CLE_delay_func;//call CLE delay
	
	bit clr FLAGS FLG1;

	
	//delay here - OR poll R/B# signal from FLASH
	CALL POLL_delay_func;//call POLL delay
	
	bit set FLAGS FLG3;//Disable chip - FLASH CS# de-asserted
	
	CALL Delay_Before_Return_func;
	RTS;
FLASH_PROGRAM_Command.end:
