/*
eeprom.c : User can read and write integer and float data to eeprom

Compiler: BC++ 3.1,                                                            
          Turbo C ++ 1.01(3.01) (free from http://community.borland.com/museum)
          MSC 6.0,                                                             
          MSVC 1.52.                                                           
                                                                               
Compile mode: large                                                            
                                                                               
Project: eeprom.c                                                               
         ..\Lib\(8000.Lib, 8000E.lib)   
                                                                               
Hardware: 8000

Detail description:
        To read write the eeprom,user must know the block number and address index of the data
        for 8000 system, the block number (0 to 7) 
        and there ar 256 bytes for each block
        The total bytes of the eeprom are 2048(2KBytes)
        For this demo, we will use show how to read write the integer and float data from eeprom
        
        to save interger data, it needs 2 bytes for each data        
        to save float data, it needs 4 bytes for each data
                    
[15 Nov,2006] original demo by kevin, modified by martin
----------------------------------------------------------------------
*/

#include "..\..\..\lib\8000a.h"

int Get_Integer_FromEEPROM(int iBlock,int iNo);
int Write_Integer_ToEEPROM(int iBlock,int iNo,int iValue)
{
    // Success ==> NoError
    // Failure ==> Rerurn -1
	
    int iTemp;
	
    EE_WriteEnable();
	
    EE_MultiWrite(iBlock,iNo*2,2,(char*)&iValue);
    /*
    block=iBlock
    offset=iNo*2 ===> each integer occupy 2 bytes
    byte count=2 ===> each integer occupy 2 bytes
    pointer to the data= (char*)&iValue
    */
	
    EE_WriteProtect();

    //Read back value from EEPROM to confirm the value is 
    //wrote to EEPROM successfully.
    iTemp=Get_Integer_FromEEPROM(iBlock,iNo);
    if(iTemp==iValue)
        return NoError;
    else
        return -1;
}

int Get_Integer_FromEEPROM(int iBlock,int iNo)
{
    int iValue;

    EE_MultiRead(iBlock,iNo*2,2,(char*)&iValue);
    /*
    block=iBlock
    offset=iNo*2 ===> each integer occupy 2 bytes
    byte count=2 ===> each integer occupy 2 bytes
    pointer to the data= (char*)&iValue
    */
    	
    return iValue;
}

float Get_Float_FromEEPROM(int iBlock,int iNo);
int Write_Float_ToEEPROM(int iBlock,int iNo,float fValue)
{
    // Success ==> NoError
    // Failure ==> Rerurn -1

    float fTemp;
    
    EE_WriteEnable();
    
    EE_MultiWrite(iBlock,iNo*4,4,(char*)&fValue);
    /*
    block=iBlock
    offset=iNo*4 ===> each float occupy 4 bytes
    byte count=4 ===> each float occupy 4 bytes
    pointer to the data= (char*)&fValue
    */
    
    EE_WriteProtect();
    
    //Read back value from EEPROM to confirm the value is 
    //wrote to EEPROM successfully.
    fTemp=Get_Float_FromEEPROM(iBlock,iNo);
    if((fTemp>fValue-0.001)&&(fTemp<fValue+0.001))
        return NoError;
    else
        return -1;
}

float Get_Float_FromEEPROM(int iBlock,int iNo)
{    
    float fValue;
    
    EE_MultiRead(iBlock,iNo*4,4,(char*)&fValue);
    /*
    block=iBlock
    offset=iNo*4 ===> each float occupy 4 bytes
    byte count=4 ===> each float occupy 4 bytes
    pointer to the data= (char*)&fValue
    */
    	
    return fValue;
}

void main(void)
{
    int iBlock,iNo,iRet,iValue[128];
    float fValue[64];
    
    //Initial some system information to let
    //following code can run on 40/80 CPU.
    InitLib();
    
    
    //iValue[0]=0
    //iValue[1]=10
    //iValue[2]=20
    //...
    //iValue[126]=1260
    //iValue[127]=1270
    
    // for each interger data it needs 2 bytes for each data, 
    //so the address index range (0 to 127)
    for(iNo=0;iNo<128;iNo++) 
    {
        iValue[iNo]=iNo*10;
    }
    
    //Write iValue[0]~iValue[127] to EEPROM block 1
    Print("\n\n\rWrite integer to EEPROM block 1\n\r");
    for(iNo=0;iNo<128;iNo++)   
    {
        iRet=Write_Integer_ToEEPROM(1,iNo,iValue[iNo]);
        if(iRet==0)
            Print("No%03d=ok     ",iNo);
        else
            Print("No%03d=error  ",iNo); 
        
        //Change to a new line every 5 values    
        if(((iNo+1)%5)==0)
            Print("\n\r");
    }
            
    Print("\n\n\rRead integer from EEPROM block 1\n\r");
    for(iNo=0;iNo<128;iNo++)
    {
        Print("No%03d=%04d  ",iNo,Get_Integer_FromEEPROM(1,iNo));
        
        //Change to a new line every 5 values 
        if(((iNo+1)%5)==0)
            Print("\n\r");
    }
    
    //fValue[0]=0,
    //fValue[1]=0.1
    //fValue[2]=0.2,
    //...
    //fValue[62]=6.2
    //fValue[63]=6.3
    for(iNo=0;iNo<64;iNo++)
        fValue[iNo]=(float)iNo/10.0;
    
    //Write fValue[0]~fValue[64] to EEPROM block 2
    Print("\n\n\rWrite float to EEPROM block 2\n\r");
    for(iNo=0;iNo<64;iNo++)   
    {
        iRet=Write_Float_ToEEPROM(2,iNo,fValue[iNo]);
        if(iRet==0)
            Print("No%03d=ok     ",iNo);
        else
            Print("No%03d=error  ",iNo); 
        
        //Change to a new line every 5 values    
        if(((iNo+1)%5)==0)
            Print("\n\r");
    }
            
    Print("\n\n\rRead flaot from EEPROM block 2\n\r");
    for(iNo=0;iNo<64;iNo++)
    {
        Print("No%03d=%07.3f  ",iNo,Get_Float_FromEEPROM(2,iNo));
        
        //Change to a new line every 5 values 
    	if(((iNo+1)%5)==0)
            Print("\n\r");
    }  

}