/*
 *  eeprom.c
 *
 *  v 0.0 2004.9.1 by Moki Matsushima  
 *
 *  read/write eeprom inside LinPAC
 *
 *  History
 *
 *  v 0.0 2004.9.1 by Moki Matsushima
 *    create, ... ok, i keep this as simple as possible
 *
 *  v 1.0 2016.12.8 by Winson
 *    For QC test 64 block, need i8k lib
 *
 *  v 1.1 2017.11.7 by Winson
 *    Because Write_EEP() set first parameter to zero, fix code.
 *
 *  v 1.2 2018.2.5 by Winson
 *    Write/Read 16K byte data 
 */


#include<stdio.h>
#include<stdlib.h>
#include "msw.h"


/* ------------------------------------------------------------------- */
int main(int argc, char* argv[])
{
	int wRetVal, last_byte = 16384, start_byte, err_count = 0, data, x;
	DWORD temp;
	unsigned char tmp[16384] = {0};

	wRetVal = Open_Slot(0);
	if (wRetVal > 0) {
		printf("open Slot failed!\n");
		return (-1);
	}

	printf("This demo shows EEPROM write/read 16K Byte(0~16383) data.\n");

	start_byte = 0;
	data = 0;
	Enable_EEP();

	while(start_byte < last_byte)
	{
	//	printf("Write EEP byte %d = %d\n",0 + start_byte, data);
		Write_EEP(0,0 + start_byte ,(unsigned char)data);
		tmp[start_byte] = data;
		data++;

		if(data == 256)
			data = 0;
		start_byte++;
	}

	Disable_EEP();

	start_byte = 0;
	while(start_byte < last_byte)
	{
		data = Read_EEP(0, 0 + start_byte);
	//	printf("Read EEP byte %d = %d\n",0 + start_byte, (unsigned char)data);
		if(tmp[start_byte] != data) {
			printf("Read EEP byte %d = %02X error (should %02X)\n",0 + start_byte, (unsigned char)data, tmp[start_byte]);
			err_count++;
		}
		start_byte++;
	}

	if(err_count != 0)
		printf("error count %d\n",err_count);

	Close_Slot(0);
	return err_count;
}
