#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include "..\..\lib\vh2k.h"

/*

NVRAM is a 31-bytes battery backup memory storage device, which can retain data and avoid lost important information when power is off suddenly.
This demo shows how to read/write byte/short/long data.
If program needs to retain DO output result,it can use WriteShortData/WriteLongData to save DO output result when program write DO to module.
When program reset by any conditions, it can read previous output result from NVRAM as power on value.
NVRAM has 31-bytes, so it can only save 7 long data, 15 short data and 31 bytes data.

If program need more battery backup memory, it can refer to S256/S512 for i-8000 system.
S256/S512: http://www.icpdas.com/products/Remote_IO/i-8ke/s256.htm
demo: ftp://ftp.icpdas.com/pub/cd/8000cd/napdos/8000/841x881x/demo/memory/s256/

For iP-8000 system, it equipped with 512K battery backup SRAM, 
demo: ftp://ftp.icpdas.com/pub/cd/8000cd/napdos/ipac8000/demo/basic/ip-84x1_ip-88x1/misc/memory/battery_backup_sram/
*/

void WriteLongData(int index, unsigned long Data);
unsigned long  ReadLongData(int index);
void WriteShortData(int index, unsigned short Data);
unsigned short  ReadShortData(int index);



void  main()
{ 
	int i=0;
	int j;
	int errorflag=0;
	
	InitLib();
	
	
    
	for(j=0;j<31;j++)
   		WriteNVRAM(j,j);	
   		
	for(j=0;j<31;j++)
	{
		if(ReadNVRAM(j)!= j)
		{
			errorflag=1;
   			Print("\nError NVR[%d][%d]\n",j,ReadNVRAM(j));
   		}
   		else
   		{
   			Print("NVR[%d][%d]\n",j,ReadNVRAM(j));	
   		}   	
	}	
	
	if(!errorflag)
	{
		for(i=0;i<7;i++)
			WriteLongData(i,500000*i);
			
		for(i=0;i<7;i++)
			Print("L[%d]=%lu\n",i,ReadLongData(i));
			
		for(i=0;i<15;i++)
			WriteShortData(i,200*i);
			
		for(i=0;i<15;i++)
			Print("S[%d]=%u\n",i,ReadShortData(i));	
	}	
		
}


// NVRAM only has 31 bytes, it only can save 0~6 7 long Data
void WriteLongData(int index, unsigned long Data)
{
	if(index>=0 && index<7)
	{
		WriteNVRAM(index*4 + 0, (Data ) &0xff); //b0
		WriteNVRAM(index*4 + 1, (Data >>8) &0xff); //b1
		WriteNVRAM(index*4 + 2, (Data >>16) &0xff); //b2
		WriteNVRAM(index*4 + 3, (Data >>24) &0xff); //b3
	}
}
// NVRAM only has 31 bytes, it only can save 0~6 7 long Data
unsigned long  ReadLongData(int index)
{
	unsigned long Data =0;
	unsigned long b0,b1,b2,b3;
	if(index>=0 && index<7)
	{
		b0 = (unsigned long) ReadNVRAM(index*4 + 0);
		b1 = (unsigned long) ReadNVRAM(index*4 + 1);
		b2 = (unsigned long) ReadNVRAM(index*4 + 2);
		b3 = (unsigned long) ReadNVRAM(index*4 + 3);
		Data= b0 +(b1<<8 ) +(b2<<16) +(b3<<24);
	}
	return Data;
}


// NVRAM only has 31 bytes, it only can save 0~14 15 short Data
void WriteShortData(int index, unsigned short Data)
{
	if(index>=0 && index<15)
	{
		WriteNVRAM(index*2 +0 , (Data ) &0xff); //b0
		WriteNVRAM(index*2 + 1, (Data >>8) &0xff); //b1
	}
}
// NVRAM only has 31 bytes, it only can save 0~14 15 short Data
unsigned short  ReadShortData(int index)
{
	unsigned long Data =0;
	if(index>=0 && index<15)
	{
		Data= ReadNVRAM(index*2) +(unsigned long)(ReadNVRAM(index*2 + 1)<<8) ;
	}
	return Data;
}