/*******************************************************************************
--------------------------------------------------------------------------------
*** TestAll example
  
 This example uses the SCI, I2C and EEPROM of the ST72F264G2 and the REva board.
 It implements a UART (SCI) terminal that allows the other side (host PC)
  to access the EEPROM on the board through the I2C protocol.
 You can duplicate and modify it as a starting point for your projects.
 
--------------------------------------------------------------------------------
*** FILES

 The project has been initially generated by RBuilder, and then modified.
 The modified files, which you will be interested in, are:
  eeprommain.c (this file) which contains the initializations and the main loop.
  revalib.c which contains utility functions for the REva.
  
--------------------------------------------------------------------------------
*** DEBUG

 If you want to debug using ICC, you must disable the watchdog
  using the DEBUGICC macro.
 
--------------------------------------------------------------------------------
*** JUMPERS

 On the REva board, you must set some jumpers:
 COM: SCL, SDA, TX, RX, EN/EEPROM, pullups/I2C
 
 On the daughter board, select the RLink clock by setting the "DNG" jumper.
 
--------------------------------------------------------------------------------
********************************************************************************
--------------------------------------------------------------------------------

			Copyright (c), RAISONANCE

--------------------------------------------------------------------------------
 The present source code which is for guidance only aims at providing
 customers with information regarding their products in order for them to
 save time. As a result, RAISONANCE shall not be held liable for any
 direct indirect or consequential damages with respect to any claims arising
 from the content of such a source code and/or the use made by customers of 
 the information contained herein in connection with their products.
--------------------------------------------------------------------------------
*******************************************************************************/
 

/* =========================================================================== */
/* Project:        EEPROM */
/* File:           EEPROMPeriphInit.c */
/* Organization:    */
/* Author:           (initial version generated by RBuilder) */
/* Date:           12/9/2004 */
/* =========================================================================== */

#include "ST7lib_config.h"
#include <stdio.h>
#include "I2C_hr.h"

#include "REvaLib.h"

//define DEBUGICC at 1 if you plan to debug using ICC
// this disables the watchdog and SPI
#define DEBUGICC 1
//#define DEBUGICC 0

/* --------------------------------------------------------------------------- */
/* Function:       PeriphInit */
/* Purpose:        Periph configuration at RESET */
/* Date:           12/9/2004 */
/* --------------------------------------------------------------------------- */
void PeriphInit ( void ) 
{
   
   /* SCI initialization */
   SCI_Init(SCI_DEFAULT_PARAM1, SCI_DEFAULT_PARAM2);
   SCI_Select_Baudrate(0x0);
   SCIERPR =  0x27;  /* Set RX Baudrate (real= 9615)  */
   SCIETPR =  0x27;  /* Set TX Baudrate (real= 9615)  */
   SCI_Mode(SCI_TX_ENABLE|SCI_RX_ENABLE);
   
   /* I2C initialization */
   I2C_Init(I2C_ACK_ENABLE);
   I2CCCR = 0x88;    /* I2C Clock configuration */
   
   /* Watch Dog initialization */
#if DEBUGICC
#define WDGREFRESH
#else
#define WDGREFRESH WDG_Refresh(0x40)
#endif
   WDGREFRESH;
   
   
}/* end of  PeriphInit */

//this function reads a byte in hex from the SCI
//  that's two hex digits for one unsigned char
unsigned char readhex2()
{
	char car1;
	char car2;
	
	car1=getchar(); //read high weight nibble
	car1=readhex(car1); //convert in hex number
	
	car2=getchar(); //read low weight nibble
	car2=readhex(car2); //convert in hex number
	
	return ((car1<<4)+car2);
}

//if zero, getchar does not send an echo. if not zero, it does.
unsigned char Echo_ON=1;



/* --------------------------------------------------------------------------- */
/* Function:       putchar */
/* Purpose:        Putchar in polling mode on SCI */
/* Date:           12/9/2004 */
/* --------------------------------------------------------------------------- */
int putchar(char c)
{
   if (c == '\n')
   {
      SCI_PutByte ('\r');
      while (!SCI_IsTransmitCompleted());
   }
   SCI_PutByte (c);
   while (!SCI_IsTransmitCompleted());
   return ((int)c);
}

/* --------------------------------------------------------------------------- */
/* Function:       getchar */
/* Purpose:        Getchar in polling mode on SCI */
/* Date:           12/9/2004 */
/* --------------------------------------------------------------------------- */
int getchar(void)
{
   unsigned char c;
   while(SCI_IsReceptionCompleted()!=SCI_RECEIVE_OK) WDGREFRESH;
   c = SCI_GetByte();
   if(Echo_ON) putchar(c); //echo
   return((int)c);
}




/* --------------------------------------------------------------------------- */
/* Function:       main() */
/* Purpose:        main routine */
/* Date:           12/9/2004 */
/* --------------------------------------------------------------------------- */
void main ( void ) 
{
   //this buffer is used for storing temp data (mainly from/to the EEPROM)
	unsigned char mybuffer[0x20];

   /* Configure the internal peripherals */
   PeriphInit();


  	printf("\n *** REva EEPROM test program ***");

   while ( 1 )
   {
		char c;
		
		//display the prompt
	  	printf("\n>");
	  	
   	//read the command
   	c=getchar();
   	
   	// analyze and execute the command
   	switch(c)
   	{
	   	
			// **** Memory access functions
			// note that these allow to access the Registers
			// so they could make some of the other commands crash
			// by unconfiguring some peripherals.
	   		
	   	case 'R': //read EEPROM or RAM or code or ...
	   	case 'W': //write EEPROM or RAM or code or ...
	   		c-='A'; //make it lower case
	   		c+='a';
	   	case 'r': //read EEPROM or RAM or code or ...
	   	case 'w': //write EEPROM or RAM or code or ...
		   	{
					char d; //that's the character indicating which space to read or write
					unsigned char cnt; //that's the number of bytes to read or write
					unsigned char tmpcnt; //that's the counter of bytes read or written
					unsigned short realaddr; //that's the address of the data in its original space
					unsigned char *addr; //that's the pointer to the RAM/code space where we put or get the data
					
		   		//read destination space ('e' for EEPROM, and 'm' for memory)
		   		d=getchar();
		   		
		   		//read address
		   		realaddr = readhex2() * 0x100;
		   		realaddr += readhex2();
		   		
		   		//read count
		   		cnt = readhex2();
		   		
		   		//use the buffer if accessing the EEPROM, or directly the code/RAM space
		   		switch(d)
		   		{
		   			case 'E':
		   				d='e';
		   			case 'e':
		   				addr=mybuffer;
		   				//the EEPROM has only 0x100 bytes
		   				realaddr&=0xff;
		   				//the buffer is small, so we can't process more than 0x20 bytes at a time.
		   				if(cnt>0x20) cnt=0x20;
		   				break;
		   			case 'M':
		   				d='m';
		   			case 'm':
		   				addr=(unsigned char *)(realaddr);
		   				break;
		   		}
	   		
		   		if(c=='w')
		   		{
		   			//if writing
		   			//read the values to write
		   			tmpcnt=0;
		   			while(tmpcnt<cnt)
				   		addr[tmpcnt++] = readhex2();
			   		
			   		//write the EEPROM if needed
			   		if(d=='e')
							WriteEEPROM((unsigned char)(realaddr&0xFF), cnt, addr);
		   		}
		   		else
		   		{
		   			//if reading...
		   			//read the values from the EEPROM if needed
			   		if(d=='e')
			   			ReadEEPROM((unsigned char)(realaddr&0xFF), cnt, addr);
		   			
		   			//display the values
			   		putchar('\n');
		   			tmpcnt=0;
		   			while(tmpcnt<cnt)
		   			{
			   			printf(" %02X",(int)addr[tmpcnt++]);
			   		}
		   		}
	   		
		   	}

	   		putchar('\n');
   		
	   		break;
		
			// **** Misc. functions
			
	  // 	case 'D': //test watchdog
	  // 	case 'd':
	  // 		c=' ';
	   		// this infinite loop does not refresh the watchdog.
	   		// therefore, the wdg will reset the device
	   		// a short time after this loop starts executing!
	   		// when debugging, you can use this to test breakpoints and STOP,
	   		// but not the watchdog because it makes the debugger crash.
	   //		while(1)
	   //			putchar(c++);
	   	//	break;
	   	
	   	case 'E': //toggle echo on SCI
	   	case 'e':
	   		Echo_ON=1-Echo_ON;
	   		break;
	   		
	   	default:
	   		//unknown command. print the help.
			  	printf("\n *** REva EEPROM test program ***");
	   	printf("\n");
		   	printf("\nRMaaaacc: read memory (4 hex digits for a, 2 for c).");
		   	printf("\nRE00aacc: read EEPROM (2 hex digits for a, 2 for c).");
		   	printf("\nWMaaaaccxxx...: write memory (2 hex digits for every byte).");
		   	printf("\nWE00aaccxxx...: write EEPROM (2 hex digits for every byte).");
		   	printf("\n");
		   	printf("\nD: test watchdog.");
		   	printf("\nE: toggle echo on SCI.");
		   	printf("\n");
		   	printf("\n");
	   		break;
   	}

   }


}/* end of main */

