#include <string.h>
#include <startup.h>
#include <LPC214X.h>
#include <armVIC.h>
#include <TFT018.h>
#include "ff.h"
#include "diskio.h"
#include <irq.h>

#define CPU_CLOCK   60000000                /* CPU Clock 6MHz     */
#define FREQ       100                      /* Frequency 100Hz    */
#define TIM_TICK    (CPU_CLOCK/FREQ)        /* Timer Tick         */
#define	INT_VECT_NUM	0

/*----------------------------------------------------------*\
 | Realtime Initial                                         |
\*----------------------------------------------------------*/
void  RTCInit(void) {
	PCONP  |= 0x00000200;   
	CCR     = 0x00000002;
	CIIR    = 0x00000000;
	AMR     = 0x000000FF;
	ILR     = 0x00000003;
	//PREINT  = FPCLK / 32768 - 1;
	//PREFRAC = FPCLK - (FPCLK / 32768) * 32768;
	CCR     = 0x00000011;
	}

DWORD get_fattime ()
{
	return	((YEAR-1980) << 25)	      // Year = 2006
			| (MONTH << 21)	      // Month = Feb
			| (DOM << 16)	      // Day = 9
			| (HOUR << 11)	      // Hour = 22
			| (MIN << 5)	      // Min = 30
			| (SEC >> 1)	      // Sec = 0
			;
}


static void tc0(void) __attribute__ ((interrupt("IRQ")));
void tc0 (void) {
	disk_timerproc();
  T0IR        = 1;                          /* Clear interrupt flag           */
  VICVectAddr = 0;                          /* Acknowledge Interrupt          */
}

void initTimer0(){
  /* Setup Timer 0: Periodic Interrupt @ DATA_FREQ Rate                       */
  T0MR0 = TIM_TICK - 1;                     /* Timer0 Match Value 0           */
  T0MCR = 3;                                /* Interrupt and Reset on MR0     */
  T0TCR = 1;                                /* Timer0 Enable                  */

	VICIntSelect &= ~(1<<TIMER0_INT);
	VICIntEnable |= (1<<TIMER0_INT);

	(*(&VICVectCntl0+INT_VECT_NUM)) = 0x20 | TIMER0_INT; // choose highest priority ISR slot 	
	(*(&VICVectAddr0+INT_VECT_NUM)) = (int)tc0;

	enableIRQ();
}


char s[20];
int main (void)
{
  FATFS fs; 
  FIL F;
  FRESULT fres;
  UINT bytesWritten;
  const char filename[] = "test.txt";
  const char buf1[] = "Hello world!\nIf you can read this text, the filesystem module is working!\n";

	Initialize();

	BacklightOn();
	LCD_Init();
	LCD_Clear_Screen(Blue);

	Font = 0;
	LCD_PutString(16,5,"LPC2148 SD FatFs",Cyan,Blue);

	RTCInit();
	initTimer0();

  f_mount(0, &fs);

  fres = f_open (&F, filename, FA_READ | FA_WRITE | FA_CREATE_ALWAYS);
  if(!fres){
	LCD_PutString(0,5+16,"Create test.txt OK",Green,Blue);
  }
  else{
	LCD_PutString(0,5+16,"Create test.txt Error!",Red,Blue);
	while(1);
  }
  //printf("status f_open: %d\n\r", fres); //status 0 = success
  f_sync (&F);

  fres = f_write (&F, buf1, sizeof (buf1) - 1, &bytesWritten);
  f_sync (&F);
  if(!fres){
	LCD_PutString(0,5+16*2,"Write test.txt  OK",Green,Blue);
  }
  else{
	LCD_PutString(0,5+16*2,"Write test.txt  Error!",Red,Blue);
	while(1);
  }
  //printf("status f_write: %d\n\r", fres); //status 0 = success

  f_close (&F);

  fres = f_open (&F, filename, FA_READ);
  if(!fres){
	LCD_PutString(0,5+16*3,"Open test.txt   OK",Green,Blue);
  }
  else{
	LCD_PutString(0,5+16*3,"Open test.txt   Error!",Red,Blue);
	while(1);
  }
  //printf("status f_open: %d\n\r", fres); //status 0 = success
  f_sync (&F);

  fres = f_read (&F, s, 12, &bytesWritten);
  f_sync (&F);
  if(!fres){
	LCD_PutString(0,5+16*4,"Read test.txt   OK",Green,Blue);
  }
  else{
	LCD_PutString(0,5+16*4,"Read test.txt   Error!",Red,Blue);
	while(1);
  }
  //printf("status f_write: %d\n\r", fres); //status 0 = success
  f_close (&F);

  s[bytesWritten] = 0;
  LCD_PutString(0,5+16*5,"test.txt:(12Bytes)",Red,Blue);
  LCD_PutString(0,5+16*6,s,Yellow,Blue);

  f_mount(0, NULL);
  while(1);

}
