//
//     XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
//     SOLELY FOR USE IN DEVELOPING PROGRAMS AND SOLUTIONS FOR
//     XILINX DEVICES.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION
//     AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION
//     OR STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS
//     IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
//     AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
//     FOR YOUR IMPLEMENTATION.  XILINX EXPRESSLY DISCLAIMS ANY
//     WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
//     IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
//     REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
//     INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//     FOR A PARTICULAR PURPOSE.
//     
//     (c) Copyright 2004 Xilinx, Inc.
//     All rights reserved.
//

/* 
 *  Simple test for read/write to Compact Flash
 *
 *  	Tries to reads the following files:
 *	"a:\\test.txt";
 *	"a:\\xflow.log";
 *	"a:\\sysfat.doc";
 *	"a:\\uclinux.zip";
 *	
 *	If CONFIG_WRITE is enabled, additionally writes:
 *	"a:\\parse.c";
 *
 *	If CONFIG_DIR_SUPPORT is enabled, additionally creates:
 *	"a:\\talica";
 * 
 * B: & C: should be partitions on the flash disk formatted in 
 * FAT16/32. If they are FAT12, then FAT12 support should be configured 
 * 
*/ 
#include <xparameters.h>   
#include <sysace_stdio.h>        
#include "xuartns550_l.h"

#include "util.h"

#define CONFIG_WRITE
#define CONFIG_DIR_SUPPORT
#define UART_BASEADDR   XPAR_OPB_UART16550_0_BASEADDR
#define UART_CLOCK      XPAR_XUARTNS550_CLOCK_HZ
#define UART_BAUDRATE   9600

void read_file(char FileName[], int verbose)
{
	unsigned char readBuffer[512];
	SYSACE_FILE *ptest;
	int i= 0;
	int numread = 0;
	int total_bytes_read = 0;
	char file_data;
	ptest = sysace_fopen(FileName , "r" );

	if(ptest) {
		xil_printf("Reading file : %s\n\r", FileName);
		do {
			numread = sysace_fread(readBuffer, 1, 512, ptest);

			total_bytes_read = total_bytes_read + numread;
			if (verbose) {
				i = 0;
				while( i < (numread )) {
					file_data = readBuffer[i];
					xil_printf("%c", readBuffer[i]);
					i++;
				}
			} 
		} while(numread);
		sysace_fclose (ptest);        
	} else {
		xil_printf("Failed to open %s: check if file prsent\n\r", FileName);
	}

	xil_printf("Total bytes read = %d\n\r", total_bytes_read);
}    

#ifdef CONFIG_WRITE
void write_file(char FileName[], int verbose)
{
	unsigned char buf[] = "<This will be the data in the file>\n\r";
	SYSACE_FILE *ptest;
	int n = 0;
	ptest = sysace_fopen(FileName ,"w" );

	if(ptest) {
		print("Writing file contents.\n\r\n\r");
		n = sysace_fwrite(buf, 1, sizeof buf, ptest);
		xil_printf("# of bytes written: %d\n\r", n);
		sysace_fclose(ptest);
	} else {
		print("Failed to open: check if file exists\n\r");
	}
}
#endif

int main()  
{
	char file1[] = "a:\\test.txt";
	char file2[] = "a:\\xflow.log";
	char file3[] = "a:\\sysfat.doc";
	char file4[] = "a:\\uclinux.zip";

  XUartNs550_SetBaud(UART_BASEADDR, UART_CLOCK, UART_BAUDRATE);
  XUartNs550_mSetLineControlReg(UART_BASEADDR, XUN_LCR_8_DATA_BITS);


	print("\nProgram running.\n\r");
	read_file(file1, 1);
	read_file(file2, 1);
	read_file(file3, 0);
	read_file(file4, 0);
	read_file("a:\\noexist.c", 1);
	print("\n\rread done\n\r");

#ifdef CONFIG_WRITE
	write_file("a:\\parse.c", 0);
	print("write done\n\r");
#endif

#if defined(CONFIG_WRITE) && defined(CONFIG_DIR_SUPPORT)
	print("executing mkdir talica..\n\r");
	if (sysace_mkdir("a:\\talica") < 0)
		print("mkdir failed\n\r");
	else
		print("mkdir succcess\n\r");
#endif

	return 0;
}
