/*
*************************************************************************************************
*	  					NAND FLASH driver for PIC24 (16-bit)using PMP module
*										All Rights Reserved
* File name		: mainDemo.c
* Programmer 	: John Leung, TechToys Co. Hong Kong
* Web presence  : www.TechToys.com.hk
* Language		: C30 complier version 3.01, MPLAB v7.60
* Hardware		: PIC24-Eval-B2 Rev B with TY320T-240320-BO stacked on top
* Disclaimer	: THIS DRIVER IS PROVIDED "AS IS" WITH NO WARRANTY
*                 WHATSOEVER AND TECHTOYS COMPANY SPECIFICALLY DISCLAIMS ANY
*                 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
*                 A PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT.
*
*                 Copyright @2008 TECHTOYS COMPANY 
*                 All rights researved
*
* Rev			  Author	  		Date				Comment
* ---			  ------			----				-------
* 1.0a			 John Leung			19 Feb 2008			Initial Release
*************************************************************************************************
*										DESCRIPTION
*
* This module provides an interface to NAND Flash Hynix HY27US08561A for PIC24FJ128GA010
* Only basic I/O function is supported in this version. Therefore, no FAT is supported!
*
* Connect UART1 of the board to any standard RS232 monitoring program like hyperterminal
* Set the baud rate to be 19200bps, 8-data bit, 1-stop bit, no parity with no handshake.
* Follow the instruction on screen you will be able to write different content to the first
* block of the nand Flash at your wish.
* 
* Remarks:  it is important to note that prior to writing to a block, 
*			block erase has to be performed; otherwise, inaccurate data would be written to the
*			same block. One may experiment with it with different procedures than 1-2-3-4.
*************************************************************************************************
*/

#include "mainDemo.h"

// Configuration bits
// Configure fast RC oscillator with PLL and RC15 pin for Fosc/2 function	
_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_OFF)
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF)   // JTAG off, watchdog timer off

INT16U j;
INT8U dataInput;
INT16U DeviceID;
INT8U dataBuffer[NAND_PAGE_SIZE_BYTE];

void putUART1(INT8U c)
{
	while(U1STAbits.UTXBF);
	U1TXREG = c;
}

INT8U getUART1(void)
{
	while(!U1STAbits.URXDA);
	return U1RXREG;
}

int main(void)
{
	CLKDIV = 0x0000;	//FRC postscaler divided by 1 (8MHz from internal RC)

	U1BRG     = 207;	//uart baud rate of 19200bps
    U1MODE    = 0x8008;	//enable uart, BRGH=1, 1 stop bit with no parity
	U1STA     = 0x0400; //enable transmission, clear all flags
	printf("HYNIX HY27(U/S)S(08/16)561A Nand Flash Demo\r\nPlease input your action :\r\n1=Read Device ID\r\n2=Erase the first block\r\n3=Write the first page of the block with your input\r\n4=Verify the written content\r\n");

	nandInit();					//initialize NAND Flash here

	while(1)
	{	
		dataInput = getUART1();
		switch(dataInput)
		{
			case '1' : 	DeviceID = nandChkID();
						printf("DeviceId:%X\r\n",DeviceID); 
						break;
			case '2' :	//Prior to program a particular page, it is required to perform a block erase first
						nandEraseBlk(0);
						printf("Block erase complete\r\n");
						break;
			case '3' :	printf("Waiting for your input:\r\n");
						dataInput=getUART1();
						printf("You are writing '%c' to nand Flash page\r\n", dataInput);
						for(j=0;j<NAND_PAGE_SIZE_BYTE;j++) dataBuffer[j]=dataInput;
						nandWrPage(0,0, 0, &dataBuffer[0]);
						break;
			case '4' :	//clear the buffer, then refill the buffer with data read from nand Flash
						for(j=0;j<NAND_PAGE_SIZE_BYTE;j++) dataBuffer[j]=0x00;	
						nandRdPage(0,0, 0, &dataBuffer[0]);
						for(j=0;j<NAND_PAGE_SIZE_BYTE;j++) printf("Data at %u: %c\r\n", j, dataBuffer[j]);
						break;
				
			default	 :  printf("Input not valid, try again\r\n");

		}
	}

	return 0;

}
