/***********************************************************************/
/*                                                                     */
/*  FILE        :test_EEPROM.c                                         */
/*  DATE        :Thu, Jun 27, 2008                                     */
/*  DESCRIPTION :内臓EEPROMの読み書きとシリアルデータ通信テスト        */
/*  CPU TYPE    :H8/3664F                                              */
/*                                                                     */
/*  This file is generated by Renesas Project Generator (Ver.4.8).     */
/*                                                                     */
/***********************************************************************/
 
//　シリアルポートはとりあえず使えますが、エラー処理をしっかりしてありません。
//　EEPROMアクセスサブルーチンでのバイト単位の読み書きしか試していません。
                 
#ifdef __cplusplus
extern "C" {
void abort(void);
#endif
void main(void);
#ifdef __cplusplus
}
#endif

#include <machine.h>
#include "iodefine.h"
#include "LCD.h"
#include "IIC_EEPROM.h"
#include "SCI_232C.h"

unsigned char eeprom_buf[8];
volatile char hex_head[53] = "Adrs +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F\0";

/*-------------------------------------------------------------------
	512バイト　ダンプ表示
-------------------------------------------------------------------*/
void Dump_512( void )
{
	unsigned char 	buf;
	unsigned long	adrs;

	SCI_print( hex_head );
	SCI_crlf();
	adrs = 0;
	while( adrs<512 )
	{
		buf = Read_byte_EEPROM( adrs );
		if( (adrs % 16) == 0 ){
			SCI_crlf();
			SCI_print_hex( 4,adrs );
			SCI_tx( ' ' );
		} 
		SCI_print_hex( 2,buf );
		SCI_tx( ' ' );
		adrs++;
	}
} 

/*-------------------------------------------------------------------
	test 1
	アドレスの下２桁をそのアドレスに書き込む
-------------------------------------------------------------------*/
void Test_EEPROM_1( void  )
{
	unsigned char   wr_data, ng_flag;
	unsigned long	i;
	
	SCI_print("TEST-1 : byte write mode.  writedata = write address");
	SCI_crlf();
	for( i=0; i< 512; i++ )
	{
		wr_data = (unsigned char)(i & 0xff);
		ng_flag = Write_byte_EEPROM( i , wr_data );
	}
	Dump_512();
	SCI_crlf();
}

/*-------------------------------------------------------------------
	test 2
	全てのアドレスに0xFFを書き込む
-------------------------------------------------------------------*/
void Test_EEPROM_2( void )
{
	unsigned char   wr_data, ng_flag;
	unsigned long	i;
	
	SCI_print("TEST-2 : write test 0xFF");
	SCI_crlf();
	for( i=0; i< 512; i++ )
	{
		ng_flag = Write_byte_EEPROM( i , 0xFF );
	}
	Dump_512();
	SCI_crlf();
}

/*-------------------------------------------------------------------
	test 3
	全てのアドレスに0x00を書き込む
-------------------------------------------------------------------*/
void Test_EEPROM_3( void )
{
	unsigned char   wr_data, ng_flag;
	unsigned long	i;
	
	SCI_print("TEST-3 : write test 0x00");
	SCI_crlf();
	for( i=0; i< 512; i++ )
	{
		ng_flag = Write_byte_EEPROM( i , 0x00 );
	}
	Dump_512();
	SCI_crlf();
}

/*-------------------------------------------------------------------
	test 4
	read -> 反転 -> write を連続して行う
-------------------------------------------------------------------*/
void Test_EEPROM_4( void )
{
	unsigned char   wr_data, ng_flag;
	unsigned long	i;
	
	SCI_print("TEST-4 : alt read & write");
	SCI_crlf();
	for( i=0; i< 512; i++ )
	{
		wr_data = Read_byte_EEPROM( i );
		wr_data = ~wr_data;
		ng_flag = Write_byte_EEPROM( i , wr_data );
		wait_e(10000);			// IIC_EEPROM.h にあるウェイトルーチン		
	}							// 書き込み後、約10msのウェイトが無いと正常に動作しないので注意
	Dump_512();
	SCI_crlf();
}


/*;****************************************************/
/*; Main Program                                      */
/*;****************************************************/

void main(void){
	
	unsigned char i;
	unsigned char rx_data;
	LCD_init();									//　LCDモジュールのイニシャライズ
	SCI_init();									//　シリアル通信のイニシャライズ
	EEPROM_init();								//　I2Cバスのイニシャライズ
	
	while (1)
	{
		LCD_lineprint( 1,1,"EEPROM TEST     " );
		SCI_print( "AKI-3664N EEPROM test program" );
		SCI_crlf();
		Dump_512();								//　EEPROM 512バイトのダンプ表示
		while(1)
		{
		SCI_crlf();
		SCI_print( "1 : byte write test" );
		SCI_crlf();
		SCI_print( "2 : write test 0xFF" );
		SCI_crlf();
		SCI_print( "3 : write test 0x00" );
		SCI_crlf();
		SCI_print( "4 : alt read & write" );
		SCI_crlf();
		SCI_print( "Input test No. = " );

			rx_data = SCI_rx();					//　シリアルより１文字取得
			SCI_tx( rx_data );					//　入力文字のエコーバック
			
			SCI_crlf();
			SCI_crlf();
			switch( rx_data )
			{	case '1':
					Test_EEPROM_1();
					break;
				case '2':
					Test_EEPROM_2();
					break;
				case '3':
					Test_EEPROM_3();
					break;
				case '4':
					Test_EEPROM_4();
					break;
			}
			rx_data = 0;
			SCI_crlf();
		}
	}
}
 

#ifdef __cplusplus
void abort(void)
{
	
}
#endif
